RosettaCodeData/Task/Associative-array-Iteration/Ruby/associative-array-iteration-1.rb

15 lines
390 B
Ruby
Raw Permalink Normal View History

2016-12-05 22:15:40 +01:00
my_dict = { "hello" => 13,
2013-04-10 14:58:50 -07:00
"world" => 31,
"!" => 71 }
# iterating over key-value pairs:
2016-12-05 22:15:40 +01:00
my_dict.each {|key, value| puts "key = #{key}, value = #{value}"}
2013-04-10 14:58:50 -07:00
# or
2016-12-05 22:15:40 +01:00
my_dict.each_pair {|key, value| puts "key = #{key}, value = #{value}"}
2013-04-10 14:58:50 -07:00
# iterating over keys:
2016-12-05 22:15:40 +01:00
my_dict.each_key {|key| puts "key = #{key}"}
2013-04-10 14:58:50 -07:00
# iterating over values:
2016-12-05 22:15:40 +01:00
my_dict.each_value {|value| puts "value =#{value}"}