Professional Documents
Culture Documents
Ruby
BY DEEPA ACHARYA
14.1 Origins and Uses of Ruby
Float literals may not have a decimal point first or last. The decimal point
must be both preceded and followed by at least one digit. So, .435 is not
a legal literal in Ruby.
14.2 String Literals
Single or double quoted
Double quoted has escape characters and expression
interpolation
Single quoted have not escape characters or expression
interpolation
q$...$ for a single quoted string
$ can be other characters
q<Don’t you think flower’s pretty?>
<{([ as the start delimiter must be matched by >})] at the end
Q$...$ for a double quoted string
Q@”Why not learn Ruby?”, he asked.@
The null string (the string with no characters) can be denoted
with either ‘‘ or ““.
14.2 Variables and Assignment
Statements
Variables are not formally declared and do not have a fixed
type
An identifier begins with a lower case letter or underscore
followed by letters(uc or lc) and/or digits and/or underscore
Variables are all references to objects
An unassigned variable has value nil
Named constants are named like variables but have names
that begin with a capital letter
There are some implicit variables such as $_,$^,$\ etc
The letters in a variable name are case sensitive, meaning
that fRIZZY, frizzy, frIzZy, and friZZy are all distinct names.
The results of executing any Ruby code can be included by
placing the code in braces and preceding the left brace with a
pound sign (#).
For example, if the value of tue_high is 83, then the string
“Tuesday’s high temperature was #{tue_high}”
has the following value:
“Tuesday’s high temperature was 83”
Similarly, if the value of price is 1.65 and that of quantity is 6, then
the value of the string
“The cost of our apple order is $#{price * quantity}”
is
“The cost of our apple order is $9.90”
14.2 Numeric Operators
Standard operators: + - * / %
Integer/Integer is integer
** for exponentiation
Missing: ++ and –
Math module: cos, sin, log, sqrt, …
The interactive Ruby interpreter IRB takes
experssions and responds with the values
Interactive Ruby
Simply type irb at the command prompt in the directory that
contains the Ruby interpreter. For example, if the command
prompt is a percent sign (%), one can type
% irb
after which irb will respond with its own prompt, which is
irb(main):001:0>
>>mystr="Good "
=> "Good "
>>mystr<<"Morning"
=> "Good Morning"
If mystr is assigned to another If a different string is
variable, that variable will assigned to mystr, Ruby
reference the same memory will build a memory
location as mystr:
location with the value of
the new string literal and
mystr will reference that
location. But yourstr will still
reference the location
with “Wow!”:
Replace method makes references to refer the same memory
location
>>st1="Good"
=> "Good"
>>st2=st1
=> "Good"
>>st1.replace("Bad")
=> "Bad"
>>st1
=> "Bad"
>>st2
"Bad“
A Boolean expression
Any value but nil is considered true
Comparison operators
Usual: == != < > <= >=
Compare for order: <=>
Compare for type and value: eql?
Compare for equality of object: equal?
Operator precedence and associativity
Operator Precedences
14.4 Selection and Loop Statements
If statement
if control-expression
statements
elsif control-expression
statements
else
statements
end
elsif can be repeated any number of times
(including 0)
14.4 Unless
case expression
when value then
- statement sequence
when value then
- statement sequence
[else
- statement sequence]
end
No break statements are needed at the ends of the
sequences of selectable statements in this construct:
There are implied branches at the end of each
when clause that cause execution to exit the
construct.
14.4 Case Expression
Syntax
case
when Boolean expression then expression
...
when Boolean expression then expression
else expression
end
The first true Boolean expression causes the matching
expression to be evaluated as the value of the case
The else expression is used if none of the Booleans are true
The Boolean expressions are evaluated
one at a time, until one evaluates to true.
The value of the whole construct is the
value of the xpression that corresponds to
the true Boolean expression.
If none of the Boolean expressions is true,
the else expression is evaluated and its
value is the value of the construct.
14.4 Loops
While loop repeats while control expression is true
Until loop repeats until control expression is true
loop introduces an infinite loop
A code block is a sequence of statements
delimited by braces or by keywords begin and
end
The loop keyword is followed by a code block
Exit from the loop is by the break statement
The next statement causes control to go back to
the beginning of the code block
14.5 Fundamentals of Arrays
Ruby arrays are dynamic in size and can store different types of data in
different elements
Creating an array
Array.new(size)
Array.new(size, value)
push
concat
Reverse
reverse!
The include? predicate method searches an
array for a specific object:
The sort method sorts the elements of an array.
Ruby can compare numbers with numbers and strings
with strings. So, sort works well on arrays of elements of
either of these two types:
Subscripting
Emptying hash
Hask_key? Method
Method syntax
def name [( formal-parameters )]
statements
end
Parentheses can be omitted if there are no formal
parameters
Return statement ends execution of the method
With a value, it returns the value as the value of the
method
If no return ends a method, the last expression is the
value of the method
14.7 Local Variables
Gsub! mutator
i modifier
Introduction to
Rails
15.1 Overview of Rails
Rails is Ruby based
“A development framework for Web-based applications”
Rails uses the Model-View-Controller architecture
Model classes are responsible for maintaining data.
View classes present the data to the user
Controller classes perform computations and deal with user
interaction
Rails uses an Object-Relational Mapping approach to working
with databases
A cars table corresponds to a car class
The rows of the table correspond to instances of the class
The correspondence is built automatically by the Rails framework
Rails uses a combination of Ruby code and template files to
create responses
15.1 Developer Tasks