Forum: Ruby dynamic object creation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ken Tyler (Guest)
on 2009-05-25 18:58
sorry if i've missed something obvious

is there a way in ruby to dynamically create an object from a variable
that holds the object's name?

something that does object.create("some_object_name")
68814ed988eee4f21b2cd07353e5e445?d=identicon&s=25 Mk 27 (mk27)
on 2009-05-25 19:21
What's the difference between that and

somename = ObjectClass.new
146e52d49d361f85c0945487452fc6a0?d=identicon&s=25 Ben Lovell (benl)
on 2009-05-25 19:23
(Received via mailing list)
On Mon, May 25, 2009 at 5:58 PM, Ken Tyler <ken@8thfold.com> wrote:

> sorry if i've missed something obvious
>
> is there a way in ruby to dynamically create an object from a variable
> that holds the object's name?
>
> something that does object.create("some_object_name")
>

Try:

Kernel.const_get("some_object_name")
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2009-05-25 19:24
(Received via mailing list)
Ken Tyler wrote:
> sorry if i've missed something obvious
>
> is there a way in ruby to dynamically create an object from a variable
> that holds the object's name?
>
> something that does object.create("some_object_name")

Assuming you mean "a variable that holds the class's name", check out
Module.const_get. This method takes a string and returns the value of
the constant having that name. Since the value of the constant Array is
the Array class object, we can get the Array class object like this:

cls = Module.const_get("Array")

And then call new on the class object to create a new array:

ary = cls.new
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-05-25 19:26
Ken Tyler wrote:
> sorry if i've missed something obvious
>
> is there a way in ruby to dynamically create an object from a variable
> that holds the object's name?

That doesn't make sense - if the object has a name then surely it
already exists?

Perhaps you wish to create an object where a variable holds the name of
the *class* you wish to create a new instance of. In that case:

  klassname = "String"
  klass = Object.const_get(klassname)
  foo = klass.new

If the class name contains :: scope separator, then you need to split on
that first.

  klass = klassname.split('::').inject(Object) { |base,item|
    base.const_get(item)
  }
Ken Tyler (Guest)
on 2009-05-25 19:37
thanks for all the responses

i'm just learning ruby so sometimes i don't know which end of the
screwdriver to grab
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-05-25 21:47
(Received via mailing list)
On 25.05.2009 19:37, Ken Tyler wrote:
> thanks for all the responses
>
> i'm just learning ruby so sometimes i don't know which end of the
> screwdriver to grab

Hint: if it hurts you got the wrong end. ;-)

Kind regards

  robert
This topic is locked and can not be replied to.