Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dynamic object creation

3 views
Skip to first unread message

Ken Tyler

unread,
May 25, 2009, 12:58:44 PM5/25/09
to
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")
--
Posted via http://www.ruby-forum.com/.

Mk 27

unread,
May 25, 2009, 1:21:31 PM5/25/09
to
What's the difference between that and

somename = ObjectClass.new

Ben Lovell

unread,
May 25, 2009, 1:22:27 PM5/25/09
to
[Note: parts of this message were removed to make it a legal post.]

On Mon, May 25, 2009 at 5:58 PM, Ken Tyler <k...@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")

--
Regards,
Ben

Tim Hunter

unread,
May 25, 2009, 1:22:30 PM5/25/09
to
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


--
RMagick: http://rmagick.rubyforge.org/

Brian Candler

unread,
May 25, 2009, 1:26:45 PM5/25/09
to
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

unread,
May 25, 2009, 1:37:52 PM5/25/09
to
thanks for all the responses

i'm just learning ruby so sometimes i don't know which end of the
screwdriver to grab

Robert Klemme

unread,
May 25, 2009, 3:42:29 PM5/25/09
to
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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

0 new messages