------ art_5728_24702541.1143193194300
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Something like this might work
---------------------------------- 8< --------------------------
#!/usr/bin/env ruby
testhash = {
:outside => 1,
"recipeKey"=>{"title"=>"RailsBook", "category_id"=>"1",
"description"=>"This is a book about Rails", "instructions"=>"Read It",
"author" => { :name => "Robert", :email => "<robert.dober / gmail.com>" }
},
:out_again => true
}
def traverse( aHash, level = 0 )
aHash.each do
|k, v|
puts "%s%s:%s" % [ " " *level, k,
v.kind_of?(Hash) ? traverse( v, level + 1) : v
]
end # do
""
end # def traverse(
traverse testhash
---------------------------------- >8 --------------------------
hope that helps
Robert
On 3/24/06, Dinesh Dinesh <u_dinesh / yahoo.com> wrote:
>
> Hi,
>
> I'm new to Ruby. I want to know how to iterate over a nested hash as
> below.
>
>
> *****************************************************************************
> testhash = {
>
> "recipeKey"=>{"title"=>"RailsBook", "category_id"=>"1",
> "description"=>"This is a book about Rails", "instructions"=>"Read It"}
>
> }
> testhash.each {|key, value| print key, " value is : ", value, "\n" }
>
> Here is the output i get..
>
> ==> recipeKey value is : titleRailsBookdescriptionThis is a book about
> Railscategory_id1instructionsRead It
>
> *****************************************************************************
>
> As per the input "testhash", the value itself is a hash rt with keys as
> "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?
>
> 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? so wanted
> to know how to iterate the above nested hash ? Please help me out.
>
> Thank You..
>
> --
> Posted via http://www.ruby-forum.com/.
>
>
--
Deux choses sont infinies : l'univers et la bóŐise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.
- Albert Einstein
------ art_5728_24702541.1143193194300--