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
Create instance of class. Class name stored in a string.
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
  2 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
 
shaw.c...@gmail.com  
View profile  
 More options Sep 24 2012, 8:17 pm
Newsgroups: comp.lang.ruby
From: shaw.c...@gmail.com
Date: Mon, 24 Sep 2012 17:17:32 -0700 (PDT)
Local: Mon, Sep 24 2012 8:17 pm
Subject: Create instance of class. Class name stored in a string.
Hi, I'm super new in Ruby World, and got in trouble with that:

how do I call the 'new' method of a class I don't know the name? The name of the class is stored in a string.

something like:

arg1 = "RADIUS"
codec = arg1.new

I would like to create an instance of whatever class name is stored in arg1 string.

Thanks in advance.

-- sfim


 
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 Sep 25 2012, 2:42 am
Newsgroups: comp.lang.ruby
From: Robert Klemme <shortcut...@googlemail.com>
Date: Tue, 25 Sep 2012 08:42:10 +0200
Local: Tues, Sep 25 2012 2:42 am
Subject: Re: Create instance of class. Class name stored in a string.
On 25.09.2012 02:17, shaw.c...@gmail.com wrote:

> Hi, I'm super new in Ruby World, and got in trouble with that:

> how do I call the 'new' method of a class I don't know the name? The
> name of the class is stored in a string.

> something like:

> arg1 = "RADIUS" codec = arg1.new

> I would like to create an instance of whatever class name is stored
> in arg1 string.

The usual idiom which also works with nested names is like this:

cls = name.split('::').inject(Object) {|cl, part| cl.const_get part}

Now you can do

obj = cls.new

In your case it's sufficient to do

cls = Object.const_get(arg1) # "RADIUS"
codec = cls.new

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 »