Skip to content

Instantly share code, notes, and snippets.

@TravisL12
Last active December 24, 2015 08:29
Show Gist options
  • Select an option

  • Save TravisL12/6770506 to your computer and use it in GitHub Desktop.

Select an option

Save TravisL12/6770506 to your computer and use it in GitHub Desktop.
# Understanding the 'each' method
# Array Example
# When using an array you iterate with only one item at a time. Thus
# on line 7 we only need the |item| to loop over.
restaurant_menu = ["Ramen", "Dal Makhani", "Coffee"]
restaurant_menu.each do |item|
puts item
end
# Hash Example
# When you have a hash you have two items to iterate over |item, price|
restaurant_menu = { "Ramen" => 3, "Dal Makhani" => 4, "Coffee" => 2 }
restaurant_menu.each do |item, price|
puts item
puts price
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment