Hash two dimensions

M

Mario Ruiz

Hi Everybody!
I'm doing this:
$Config=Hash.new(Hash.new())
$Config["UNO"]["urlX"]="beep/UNO/playdoit.do"
$Config["UNO"]["urlY"]="beep/UNO/dontplayPP.do"
$Config["UNO"]["urlZ"]="beep/UNO/dontplay34jo.do"
$Config["DOS"]["urlX"]="beep/DOS/playad.do"
$Config["DOS"]["urlY"]="beep/DOS/dontplayf.do"
$Config["DOS"]["urlZ"]="beep/DOS/dontplay34aa.do"
$Config["TRES"]["urlX"]="beep/TRES/playcs.do"
$Config["TRES"]["urlY"]="beep/TRES/dontplayff.do"
$Config["TRES"]["urlZ"]="beep/TRES/dontplay34s.do"

when I try to access to the value in $Config["UNO"]["urlY"] the result
is the value in $Config[TRES"]["urlY"]

Why??

Thanks a lot.
 
A

Austin Ziegler

Hi Everybody!
I'm doing this:
$Config=Hash.new(Hash.new())
$Config["UNO"]["urlX"]="beep/UNO/playdoit.do"
$Config["UNO"]["urlY"]="beep/UNO/dontplayPP.do"
$Config["UNO"]["urlZ"]="beep/UNO/dontplay34jo.do"
$Config["DOS"]["urlX"]="beep/DOS/playad.do"
$Config["DOS"]["urlY"]="beep/DOS/dontplayf.do"
$Config["DOS"]["urlZ"]="beep/DOS/dontplay34aa.do"
$Config["TRES"]["urlX"]="beep/TRES/playcs.do"
$Config["TRES"]["urlY"]="beep/TRES/dontplayff.do"
$Config["TRES"]["urlZ"]="beep/TRES/dontplay34s.do"

when I try to access to the value in $Config["UNO"]["urlY"] the result
is the value in $Config[TRES"]["urlY"]

Why??

Because it's always the same hash. What you *want* is:

$Config = Hash.new { |h, k| h[k] = Hash.new }

-austin
 
J

jandot

I had this problem a lot when I started to use ruby, especially coming
from perl with its hashes of arrays of arrays of hashes.

After a while I realized that these nested structures basically
pointed out that I should create a class to contain this type of data.
And just put those objects in an array. So for your example:

class Setting
def initialize(platform, setting, value)
@platform, @setting, @value = platform, setting, value
end
attr_accessor :platform, :setting, :value
end

configuration = Array.new
configuration.push(Setting.new("UNO", "urlX", "beep/UNO/playdoit.do"))
configuration.push(Setting.new("UNO", "urlY", "beep/UNO/
dontplayPP.do"))
....
 
T

Thomas Wieczorek

class Setting
def initialize(platform, setting, value)
@platform, @setting, @value = platform, setting, value
end
attr_accessor :platform, :setting, :value
end

configuration = Array.new
configuration.push(Setting.new("UNO", "urlX", "beep/UNO/playdoit.do"))
configuration.push(Setting.new("UNO", "urlY", "beep/UNO/
dontplayPP.do"))

You can do the same with Ruby's built-in Structs with less code:

Setting = Struct.new:)platform, :setting, :value)
configuration = Array.new
configuration.push(Setting.new("UNO", "urlX", "beep/UNO/playdoit.do"))
configuration.push(Setting.new("UNO", "urlY", "beep/UNO/dontplayPP.do"))
last = configuration.pop
puts last.platform

I also like that one
http://groups.google.de/group/comp.lang.ruby/msg/d11d68c9d3889cc5
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top