# Create named objects

**URL:** <https://www.ruby-forum.com/t/create-named-objects/54327>\
**Category:** Ruby\
**Created:** [March 1, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/create-named-objects/54327 "2006-03-01T16:36:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [March 1, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/create-named-objects/54327/1 "2006-03-01T16:36:22Z")

</div>

Hello,

want to do the following:

name = “input”  
clazz = “String”  
value = “test”

\<\<\>\>

puts(input) -\> “test”  
input.class() -\> String

Thanks in advance,

Roland

---

<div class="post-metadata">

**Author:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [March 1, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/create-named-objects/54327/2 "2006-03-01T17:42:00Z")

</div>

Hi –

On Thu, 2 Mar 2006, removed\_email\_address@domain.invalid wrote:

> > \<\<\>\>  
> > 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 (removed\_email\_address@domain.invalid)  
Ruby Power and Light ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [March 1, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/create-named-objects/54327/3 "2006-03-01T17:33:54Z")

</div>

Roland S. wrote:

> 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 )

---

<div class="post-metadata">

**Author:** ![schmitr3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea666f/32.png) [@schmitr3](https://www.ruby-forum.com/u/schmitr3)\
**Post date:** [March 1, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/create-named-objects/54327/4 "2006-03-01T20:12:27Z")

</div>

On Mar 1, 2006, at 11:33 AM, removed\_email\_address@domain.invalid wrote:

> > \<\<\>\>  
> > string
> 
> # in your insert magic here:
> 
> evs = “#{name} = #{clazz}.new( #{value} )”  
> eval( evs )

alternatively:

name = “input”  
clazz = “String”  
value = “test”

(class \<\< self; self; end).class\_eval { define\_method(name)  
{ const\_get(clazz).new(value) } }

puts(input)  
puts input.class

There are several disadvantages to this approach.

1. input is a method not really a variable and therefore its scope  
is gonna be all sorts of wrong
2. Since it is a method, assigning to it is not necessarily going to  
work the way you want

Advantages ares that yo don’t have to worry about the having seen an  
assignment to it business and you don’t have to invoke the parser

Yet another method would be  
instance\_variable\_set("@#{name}", Object.const\_get(clazz).new(value))

Unfortunately now you have to refer to it as @input. It also has  
some scoping issues.
