block is invoked in conjunction with a method.
# block is referred to as a nameless function.
# block in Ruby is often an idiom for getting all the values out of a data structure.
pacific = [ "Washington", "Oregon", "California" ]
pacific.each do |element|
puts element
end
# characters (|element|) can be any name you want.
# The block uses it as a local variable to keep track of every element in the array
# later uses it to do something with the element.
# This block uses puts to print each element in the array:
# You can replace do/end with a pair of braces: pacific.each { |e| puts e }
Related examples in the same category