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.
On Jan 22, 2008 7:42 AM, Mario R. [email protected] wrote:
$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
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”))
…
On Jan 22, 2008 2:39 PM, jandot [email protected] wrote:
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