From: Verno Miller Date: 2006-09-27T18:19:14+09:00 Subject: Re: Accessing Nested Hashes Directly > Posted by Adam Traver > on 26.09.2006 19:57 > I've been having a problem for a while with Ruby where accessing a hash > within a hash will fail if the first hash is nil. > > ... > > You will receive a hash that looks like this (within the params hash): > > "body" => { "user" => { "name" => { "given" => "Tony", "family" => "Robbins" } } } > >Thus, you can access the elements like this: > > params["body"]["user"]["name"]["given"] > > ... > > Here's my question, though: is this code efficient? Is there a better > way? Am I missing something fundamental in Ruby that would solve this > without the need for the new method? Yet another (though indirect) approach could be to first extract all keys from the nested hash! http://bigbold.com/snippets/posts/show/1908 -- Posted via http://www.ruby-forum.com/.