Accessing local variables dynamically

R

Ralph Shnelvar

[Note: parts of this message were removed to make it a legal post.]

I have a collection of variables named
@a_something, @c_something, @f_something, @y_something, @z_something


I want to do something like
def some_thing(one_char)
xxx = XXXX('@'+one_char+'_something')
subr(xxx)
end

I want
some_thing('c')
to have the same effect as
subr(@c_something)


What should XXXX be (should it be eval ...?)?

This seems to work in 1.8. Will it work in 1.9?

I have read that the dynamic creation of variables in 1.9 is prohibited ... but I'm not sure if this applies in my case since @c_something (etc.) already exists.
 
P

Paul Harrington

Ralph said:
I have a collection of variables named
@a_something, @c_something, @f_something, @y_something, @z_something


I want to do something like
def some_thing(one_char)
xxx = XXXX('@'+one_char+'_something')
subr(xxx)
end

I want
some_thing('c')
to have the same effect as
subr(@c_something)


What should XXXX be (should it be eval ...?)?

This seems to work in 1.8. Will it work in 1.9?

I have read that the dynamic creation of variables in 1.9 is prohibited
... but I'm not sure if this applies in my case since @c_something
(etc.) already exists.
instance_variable_get("@#{one_char}_something")

Though not knowing at all what you're actually trying to accomplish,
maybe you just want to use a hash instead?
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top