Sunday, August 24, 2008

Ruby notes-basic(1)

Semicolons and newline characters are interpreted as ending of a statement. +, = or \ can be use as line continuation characters.
Comments
Single-line comments: Comments extend from # to the end of a line.
Multi-line comments:

=begin
comments go here
=end
Note: '=begin' and '=end' must appear at the beginning of a line.
Integer literals: 123, 0123(octal), 0xf34(hex), 0b1123(binary), ?d(char code for 'd')
Float-point literals: 23.45, 3e4, 4E10, 5e+39
String literals:
double quotations: allow substitution and escape character sequences.
single quotations: don't allow substitution and escape character sequences except \\ and \'.
Adjacent strings are concatenated automatically. "abc""def" => "abcdef"
Command execution: `command`. The syntax is similar to the counterpart of bash. It allows substituion and escape character sequences. The output generated by execution of the command is converted to a string.
here documents:

<<EOF
content goes here
EOF
The string is in double quotations.
<<"EOF"
content goes here
EOF
<<'EOF'
content goes here
EOF
The string is in single quotations.
<<`EOF`
command goes here
EOF
The string is in back quotes.
<<-EOF
command goes here
EOF
The delimiter ('EOF' in this example) needs not be at the beginning of a line.

Symbols:
"A symbol is an object corresponding to an identifier or variable"
:name    #symbol for 'name'
:$name   #symbol for variable 'name'
Array
[], [1,2,3], [1,[2,3]]
%w(str1 str2 str3) => ["str1", "str2", "str3"]
Dictionary(Map, or Hash):
{key => value}
Regular expression:
/pattern/
/pattern/options

Alternatives:
%!string! and %Q!string! are equivalent to "string".
%q!string! is equivalent to 'string'
%x!command! is equivalent to `command`.
%r!pattern!

Variable

Type Description Example Note
global variables can be accessed globally by and program. $var Uninitialized global variables are set to nil.
instance variables that belong to an object. @var
class variables that belong to a class. @@var Class variables must be initialized before they are accessed in methods.
contants Constants as you know in other languages Var must begin with an upper case letter.May not be defined in methods.
local Local variables. var must begin with a lowercase letter or _

Variable substitutions: #{varname}. E.g. #{filename}, #{@conf}, #{$stdin} 
Variable assignments
var = value
var1, var2, ..., *varn = expr1, expr2, ..., *exprn
Operators:

expr?expr1:expr2
    The same as the tertiary operator in C/C++.
defined? varname
    #return a description about the variable/method or nil.

Control statements

Name Syntax Alternatives
if if cond [then]
    code
[elsif cond [then]
    code]
[else
    code]
end
code if cond
unless unless cond [then]
    code
[else
    code]
end
code unless cond
case case expr
[when expr [,expr ...] [then]
    code ]...
[else
    code]
end
 
while while cond [do]
    code
end
(1)code while cond
(2)begin code end while cond.
Note: code is executed once before cond is evaluated.
until until cond [do]
    code
end
(1) code until cond
(2) begin code end until cond
Note: code is executed once before cond is evaluated.
for for var[, var2 ...] in expr [do]
    code
end
expr do |var [,var...]|
    code
end
break Break from look  
next like continue in C++  
redo re-execute the loop body once without evaluation of conditional expression.  
retry re-execute a call to a method  
begin begin
    code
[rescue [exception_class [, excep_cls...]] [=>var] [then]
    code ]...
[else
    code ]
[ensure
    code ]
end
Code in ensure clause is always executed.
Code in else clause is executed only when no exceptions are raised.
Code in rescue clause is executed when exception is caught.
rescue code rescue expr Evaluate the expr only if an exception is caught.
raise raise exception_class, message
raise exception_object
raise message
raise
Raise an exception.
To call raise in a rescue clause will re-raise the exception.
BEGIN BEGIN{
    code
}
Code to be executed before the program is run.
END END{
    code
}
Code to be executed when the interpreter quits.

No comments: