Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
dynamic object creation
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ken Tyler  
View profile  
 More options May 25 2009, 12:58 pm
Newsgroups: comp.lang.ruby
From: Ken Tyler <k...@8thfold.com>
Date: Mon, 25 May 2009 11:58:44 -0500
Local: Mon, May 25 2009 12:58 pm
Subject: dynamic object creation
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/.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mk 27  
View profile  
 More options May 25 2009, 1:21 pm
Newsgroups: comp.lang.ruby
From: Mk 27 <halfcountp...@intergate.com>
Date: Mon, 25 May 2009 12:21:31 -0500
Local: Mon, May 25 2009 1:21 pm
Subject: Re: dynamic object creation
What's the difference between that and

somename = ObjectClass.new
--
Posted via http://www.ruby-forum.com/.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Lovell  
View profile  
 More options May 25 2009, 1:22 pm
Newsgroups: comp.lang.ruby
From: Ben Lovell <benjamin.lov...@gmail.com>
Date: Mon, 25 May 2009 12:22:27 -0500
Local: Mon, May 25 2009 1:22 pm
Subject: Re: dynamic object creation
[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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Hunter  
View profile  
 More options May 25 2009, 1:22 pm
Newsgroups: comp.lang.ruby
From: Tim Hunter <TimHun...@nc.rr.com>
Date: Mon, 25 May 2009 12:22:30 -0500
Local: Mon, May 25 2009 1:22 pm
Subject: Re: dynamic object creation

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/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brian Candler  
View profile  
 More options May 25 2009, 1:26 pm
Newsgroups: comp.lang.ruby
From: Brian Candler <b.cand...@pobox.com>
Date: Mon, 25 May 2009 12:26:45 -0500
Local: Mon, May 25 2009 1:26 pm
Subject: Re: dynamic object creation

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)
  }
--
Posted via http://www.ruby-forum.com/.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ken Tyler  
View profile  
 More options May 25 2009, 1:37 pm
Newsgroups: comp.lang.ruby
From: Ken Tyler <k...@8thfold.com>
Date: Mon, 25 May 2009 12:37:52 -0500
Local: Mon, May 25 2009 1:37 pm
Subject: Re: dynamic object creation
thanks for all the responses

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

--
Posted via http://www.ruby-forum.com/.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options May 25 2009, 3:42 pm
Newsgroups: comp.lang.ruby
From: Robert Klemme <shortcut...@googlemail.com>
Date: Mon, 25 May 2009 21:42:29 +0200
Local: Mon, May 25 2009 3:42 pm
Subject: Re: dynamic object creation
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/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »