ハッシュのエントリを処理する

備忘録目次 - ロバの耳

h = {
	'Apple'  => 'red',
	'Banana' => 'yellow',
	'Lemon'  => 'yellow',
	'Carrot' => 'orange'
}

h.each_key {|key| p "#{key} is #{h[key]}."}
h.each_pair {|key, val| p "#{key} is #{val}."}

# 出力結果はいずれも
#=> "Lemon is yellow."
#=> "Carrot is orange."
#=> "Apple is red."
#=> "Banana is yellow."

h.each {|x| p x}
#=> ["Lemon", "yellow"]
#=> ["Carrot", "orange"]
#=> ["Apple", "red"]
#=> ["Banana", "yellow"]