ruby loop dictionary code example
Example 1: iterate over each key value in hash ruby
restaurant_menu = { "Ramen" => 3, "Dal Makhani" => 4, "Coffee" => 2 }
restaurant_menu.each do | item, price |
puts "#{item}: $#{price}"
end
Example 2: iterating over hash, ruby
person = {name: 'bob', height: '6 ft', weight: '160 lbs', hair: 'brown'}
person.each do |key, value|
puts "Bob's #{key} is #{value}"
end
Example 3: iterate through values of an object rails
@work_profile.attributes.each do |attr_name, attr_value|
...
end