Christopher Swasey
3/4/2008 12:45:00 AM
On 3/3/08, Mariko C. <lessthaneloquent@yahoo.com> wrote:
> I'd like to change a string, for example, "something" and turn it into
> an object reference. For example:
>
> I have string "something," now I'd like to take that and turn it into
>
> something = "some other string"
>
> Is this possible? Thanks in advance for any help.
If I understand you properly, you have the name of a variable in a
string, and you want to use that string to fetch the actual variable
by that name.
What you want is Object#instance_variable_get:
@a = "@b"
@b = ["this is a test"]
instance_variable_get(@a)
=> ["this is a test"]
Note that the @ is important. You can always prepend it as needed:
instance_variable_get("@" + @a)
Christopher