Is saving a hash in another hash common practice?

Rafael picture Rafael · Jun 15, 2009 · Viewed 8.4k times · Source

I'd like to save some hash objects to a collection (in the Java world think of it as a List). I search online to see if there is a similar data structure in Ruby and have found none. For the moment being I've been trying to save hash a[] into hash b[], but have been having issues trying to get data out of hash b[].

Are there any built-in collection data structures on Ruby? If not, is saving a hash in another hash common practice?

Answer

Jonas Elfström picture Jonas Elfström · Jun 15, 2009

If it's accessing the hash in the hash that is the problem then try:

>> p = {:name => "Jonas", :pos => {:x=>100.23, :y=>40.04}}
=> {:pos=>{:y=>40.04, :x=>100.23}, :name=>"Jonas"}
>> p[:pos][:x]
=> 100.23