Keyword Examples Flashcards
Name the keyword from the given example
BEGIN { puts “hi” }
BEGIN: Run this block when the script starts
END { puts “hi” }
END: Run this block when the script is done
alias X Y
alias: create another name for a function
puts “Hello” and “Goodbye”
and: Logical and, but lower priority than &&
begin end
begin: Start a block, usually for exceptions
while true; break; end
break: break out of a loop right now
case X; when Y; else; end
case: Case style conditional, like an if
class x; end
class: Define a new class
def X(); end
def: define a new function
defined? Class == “constant”
defined?: Is this class/function/etc. defined already?
(0..5).each do |x| puts x end
do: Create a block that maybe takes a parameter
if X; else; end
else: Else conditional
if X; elsif Y; else; end
elsif: Else if conditional
begin end # many others
end: Ends blocks, functions, classes, everything
begin ensure end
ensure: Run this code whether an exception happens or not
for X in Y; end
for: For loop syntax. The .each syntax is preferred
if X; end
if: If conditional
for X in Y; end
in: In part of for-loops
module X; end
module: Define a new module
(0..5).each {|y| next }
next: skip to the next element of a .each iterator
not true == false
not: logical not. But use ! instead
puts “Hello” or “Goodbye”
or: Logical or.
(0..5).each {|i| redo if i >2 }
redo: Rerun a code block exactly the same
begin rescue X; end
rescue: Run this code if an exception happens
(0..5).each {|i| retry if i > 2 }
retry: In a rescue clause, says to try the block again
return X
return: Returns a value from a function
defined? self == “self”
self: The current object, class, module
super
super: The parent of this class
if true then puts “hi” end
then: Can be used with if optionally
undef X
undef: Remove a function definition from a class
unless false then puts “not” end
unless: Inverse of if
until false; end
until: Inverse of while, execute block as long as false
case X; when Y; else; end
when: Part of case conditionals
while true; end
while: while loop
yield
yield: Pause and transfer control to the code block