Dynamic local variables?

Hello,

Is it possible at all to generate local variables dynamically ?
What I need is roughly this:
I read in a file with one or more multidimensional hashes:

table = Tabel.new("/tmp/rubdefs.text")
puts table[“rub”][“T”][“datumontvangst”]

I want to access this as:
rub[“T”][“datumontvangst”]

If I do
rub = table[“rub”]
I have achived this, but unfortunately I don’t know the names
of the first index beforehand.
I’ve tried something like:
a = %q{ rub = table[“rub”]}
instance_eval a

but this doesn’t work. (It does with @rub instead of rub, but I prefer
locals).
Can this be done at all?

Thanks for any suggestions.

Han Holl

P.S. The reason I want this, that I’m busy trying to translate an old,
homebrewed embedded language to ruby. This language uses the
above syntax.

“Han Holl” han@pobox.com wrote in message
news:3CFD3100.3010404@pobox.com

Is it possible at all to generate local variables dynamically ?
What I need is roughly this:
I read in a file with one or more multidimensional hashes:

table = Tabel.new(“/tmp/rubdefs.text”)
puts table[“rub”][“T”][“datumontvangst”]

I want to access this as:
rub[“T”][“datumontvangst”]

Hi,

I think PHP’s extract and compact function is useful.

extract – Import variables into the current symbol table from an array
compact – Create array containing variables and their values

I implemented with ruby using global variable.

def extract(hash)
hash.each do |key,value|
eval “$#{key} = #{value.inspect}”
end
end

def compact(*arg)
hash = {}
arg.each do |key|
hash[key] = eval “$#{key}”
end
hash
end

var_hash = {“color” => “blue”, “size” => “medium”, “shape” => “sphere”}
extract(var_hash)
puts $color, $size, $shape,“\n”

var_hash2 = compact(‘color’,‘size’,‘shape’)

Park Heesob.

Park Heesob wrote:

“Han Holl” han@pobox.com wrote in message
news:3CFD3100.3010404@pobox.com

Is it possible at all to generate local variables dynamically ?
What I need is roughly this:
I read in a file with one or more multidimensional hashes:

table = Tabel.new(“/tmp/rubdefs.text”)
puts table[“rub”][“T”][“datumontvangst”]

I want to access this as:
rub[“T”][“datumontvangst”]

Hi,

I think PHP’s extract and compact function is useful.

extract – Import variables into the current symbol table from an array
compact – Create array containing variables and their values

I implemented with ruby using global variable.

Yes, my problem was with local variables.
But thanks to Guy Decoux’s excellent explanation (in the related thread you
started), I’ve found a solution involving method_missing.

Thanks for your reaction,

Han Holl