Accessing local variables dynamically

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.

Ralph Shnelvar wrote:

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?

···

--
Posted via http://www.ruby-forum.com/\.