a.each{|k,v|puts"The value for #{k} is #{v}"}# The value for a is b# The value for 3 is d# The value for 5 is a.each_key{|k|puts"The value for #{k} is #{a[k]}"}# The value for a is b# The value for 3 is d# The value for 5 is 6a.each_value{|v|putsv}# b# d# 6a.eachdo|k,v|puts"The value for #{k} is #{v}"end# The value for a is b# The value for 3 is d# The value for 5 is 6
Symbols
Fixnum always have the same object_id, and we can tell Ruby to do that to Strings too – it’s what we call a Symbol. While a new string will be created every time the same String literal is used, the same symbol will always point to the same object in memory. This also means that Ruby’s automatic garbage collection will not recycle symbols.