How to access a hash within a hash in ruby?

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

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” :slight_smile:


Agnieszka

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

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” :slight_smile:


Agnieszka

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

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

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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs