Ruby Flashcards
alias
alias :newname :oldname
Creates an alias/ second name for the method.
Thus, method defined for newname and oldname are
the same.
BEGIN
BEGIN {
` some code `
}
begin
begin
` #some code `
rescue
` #some code `
ensure
` #some code `
end
module
Creates an interface/helper that allows code to
from the module to be executed in any
other ruby files that require
the module.
next
Jumps to the next iteration in the
most internal loop.
case
case number
when 0...3
` some code`
when 4
` some code`
else
` some code`
end
Equivalent of switch statement.
defined?
Checks whether an expression is defined.
Returns false if expression isn’t defined.
retry
If retry
appears in rescue
clause of begin
expression, restart from the beginning of the begin
body.
If retry
appears in the iterator, the block, or the body of the for
expression, restarts the invocation of the iterator call.
redo
Restarts the iteration of the most internal loop.
then
Optional keyword for if statements.
i.e
if john == jack then
puts "John and Jack are twins"
end
undef
Cancels the method definition. Undef
can not appear in the method body. By using undef
and alias
, the interface of the class can be modified independently from the superclass, but notice it may be broke programs by the internal method call to self.
unless
unless $baby feed\_meat else feed\_milk end
unless
expressions are used for reverse conditional execution. It is equivalent to:
if !(cond) ... else ... end
until
Examples:
until sunrise sleep end
Syntax:
until expr [do] ... end
Executes body until condition expression returns true.
i.e. work until tired
yield
Examples:
yield data
Syntax:
yield `(' [expr [`,' expr...]]) yield [expr [`,' expr...]]
Evaluates the block given to the current method with arguments, if no argument is given, nil
is used as an argument. The argument assignment to the block parameter is done just like multiple assignment. If the block is not supplied for the current method, the exception is raised.
\a
A backslash escape
The BEL character. Rings the console bell.