From: Dinesh Umanath Date: 2006-03-24T19:28:04+09:00 Subject: Re: How to Iterate over nested Hashes ? Hi Robert, Thank a LOT for the quick reply. Sorry for not making it clear when i have posted it. What i was looking is, if the "value" is a hash, then i want to assign this value to another hash variable, so that upon iterating that hash variable i should see the key/value pairs of this new value. In our case even though the "value" was a hash i think while iterating it gets converted to string and it is printing as "continious string". I was thinking that if i create a new hash and assign the "value" (which is a hash) to this new hash, then i can iterate it. But what i feel is as you have mentioned we have to use some thing like "traverse" which you have written to manipulate it. So Is there any built-in "methods" to take care of situations like this (nested - nested hash) than writing our own "traverse" methods like this ? Again thanks for your quick and kind reply. Dinesh Robert Klemme wrote: > Dinesh Dinesh wrote: >> >> "title","category_id","description" and "instructions"? Now how to get >> these values as a key/value pair ?? >> >> If we apply "eash" operator for "testhash", the values are getting >> printed "continously string" as shown above???, is this the expected >> behaviour? > > Yes, it's the result of a hash converted to string. > >> I tried to assign this value into a "new hash" variable and when i >> applied "each" on this varibale, it said "each" varible undefined, the >> reason may be that the value which i assinged is not a hash? > > "each" is a method not a variable. > > > so wanted >> to know how to iterate the above nested hash ? Please help me out. > > First of all you should describe what you want to see during the > iteration. Do you only want to see key value pairs of nested hashes? > Do you want to see key1,key2,value? > > If you just want to print the whole Hash you can use pp: > > >> require 'pp' > => true > >> testhash = { > .... > >> pp testhash > {"recipeKey"=> > {"title"=>"RailsBook", > "description"=>"This is a book about Rails", > "category_id"=>"1", > "instructions"=>"Read It"}} > > Kind regards > > robert -- Posted via http://www.ruby-forum.com/.