Hi --

On Thu, 2 Mar 2006, kgoblin / gmail.com wrote:

>
> Roland Schmitt wrote:
>> Hello,
>>
>> want to do the following:
>>
>> name = "input"
>> clazz = "String"
>> value = "test"
>>
>> <<<some magic here>>>
>>
>> puts(input) -> "test"
>> input.class() -> String
>>
>>
>> Thanks in advance,
>>
>> Roland
>
> a pretty straight forward way is to just use the eval function on a
> string
>
> # in your insert magic here:
> evs = "#{name} = #{clazz}.new( #{value} )"
> eval( evs )

That will create a new inner scope, so if there's no variable named
input already, there won't be one when the eval exits.  (Which is
good, because otherwise we'd probably be seeing a lot of this kind of
thing :-)

The best advice, though it's not exactly an answer to the question,
is: use a hash.

   name = "input"
   value = "test"
   value_hash[name] = test

rather than the "soft reference"-style (to borrow a Perl phrase) way
of creating locals.


David

-- 
David A. Black (dblack / wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black