Forum: Ruby on Rails how to access a hash within a hash in ruby?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chris Brown (Guest)
on 2006-04-01 00:35
Simple question:

I know how to access a data field in a hash by doing
hash_name[datafield_name].

But what if the datafield itself is a hash? I tried a couple of
variations but nothing seems to be working and tutorials do not go into
such details.

Is there a simple way or should I create separate instances?

-Chris
Agnieszka F. (Guest)
on 2006-04-01 00:42
Chris Brown wrote:
>
> Simple question:
>
> I know how to access a data field in a hash by doing
> hash_name[datafield_name].
>
> But what if the datafield itself is a hash? I tried a couple of
> variations but nothing seems to be working and tutorials do not go into
> such details.
>
> Is there a simple way or should I create separate instances?
>
> -Chris

Hi,

you're not saying what you tried and maybe I missed your point but:
h = {"h1" => {"h2" => "hello"}}
h["h1"]["h2"]
gives "hello" :)

--
Agnieszka
Brian V. Hughes (Guest)
on 2006-04-01 00:45
(Received via mailing list)
Chris Brown wrote:
> Simple question:
>
> I know how to access a data field in a hash by doing
> hash_name[datafield_name].
>
> But what if the datafield itself is a hash?


hash_name[datafield_name][key_name]

-Brian
Chris Brown (Guest)
on 2006-04-01 00:50
Thanks, Agnieszka. I didn't know I could do this but I'm not sure it
will work in my case. My_hash contains standard data of an RSS file:
channel_title, channel URl and items. Items contains: title, link,
author and description.

so My_hash['title'] returns the right RSS channel Title e.g. CNN To
stories

But how can I return only all the links? or only one description? I'm
not familiar with hashes so I'm confused.

My_hash['items'][link] does not work as you suggested below.


 Figiel wrote:
> Chris Brown wrote:
>>
>> Simple question:
>>
>> I know how to access a data field in a hash by doing
>> hash_name[datafield_name].
>>
>> But what if the datafield itself is a hash? I tried a couple of
>> variations but nothing seems to be working and tutorials do not go into
>> such details.
>>
>> Is there a simple way or should I create separate instances?
>>
>> -Chris
>
> Hi,
>
> you're not saying what you tried and maybe I missed your point but:
> h = {"h1" => {"h2" => "hello"}}
> h["h1"]["h2"]
> gives "hello" :)
>
> --
> Agnieszka
Dave S. (Guest)
on 2006-04-01 01:01
(Received via mailing list)
On Friday 31 Mar 2006 21:50, Chris Brown wrote:
> My_hash['items'][link] does not work as you suggested below.

Try:

My_hash['items']['link']

You missed the quotes on 'link'...?

~Dave

--

Dave S.
Rent-A-Monkey Website Development
http://www.rentamonkey.com/

PGP Key: http://www.rentamonkey.com/pgpkey.asc
Bill W. (Guest)
on 2006-04-01 01:50
(Received via mailing list)
Hi Chris,

Chris Brown wrote:
> My_hash contains standard data of an RSS file:
> channel_title, channel URl and items.
> Items contains: title, link, author and description.
>
> so My_hash['title'] returns the right RSS channel
> Title e.g. CNN To stories
>
> But how can I return only all the links? or only one
> description? I'm not familiar with hashes so I'm confused.

I just had to work through this a week or so ago (accessing a hash of
hashes, not the RSS part) so I'll pass along what I learned in the hope
it
will help.

In general, a hash is a set of (key, value) pairs.  The value can be a
singleton value, another hash, an array...  Those values can,
themselves, be
any of the same.  To access a values in a multi-level hash, you iterate
through the levels.  So conditionally accessing a hash of hashes looks
like...

level1.each do |level1_key, level1_value|   # iterate through the top
level
set of pairs
  if level1_key == what_you_want            # test each key to see if
you
want to process it's values
    level1_value.each do |level2_key, level2_value|  # if you do, and
it's a
hash itself, iterate through it
      if level2_key == another_thing_you_want  # test each key to see if
...
        level2_value.each do |level2_key, level3_value|  # and so on
           # and down you go...
        end
      end
    end
  end
end

HTH,
Bill
Alain R. (Guest)
on 2006-04-01 15:48
(Received via mailing list)
Chris
    > I'm not familiar with hashes so I'm confused.

When in doubt, I always :
1/ check the online doc:
     http://www.ruby-doc.org/core/classes/Hash.html

2/ experiment on tryruby.
     tryruby.hobix.com/

You should also master Array and Enumerable iterations methods (reject,
inject, collect, etc..)

Alain
This topic is locked and can not be replied to.