learn_ruby_ch2_pt3 Flashcards
If statement, check for zero
value = 0
if value.zero? then
puts “value is zero.”
end
Array example (pacific)
Hash example
pacific = [ “Washington”, “Oregon”, “California”]
pacific[0] # => “Washington”
pacific = { “WA” => “Washington”, “OR” => “Oregon”, “CA” => “California” }
pacific[“OR”] # => “Oregon”
define a method (hello) and then undefine the method
def hello
puts “Hello”
end
hello # => Hello
undef hello
define a method (repeat) with arguments
def repeat( word, times )
puts word * times
end
repeat(“Hello! “, 3) # => Hello! Hello! Hello!
repeat “Good-bye! “, 4 # => Good-bye! Good-bye! Good-bye! Good-bye!
return a value
In Ruby, the last value in a method is returned, with or without an explicit return statement.