Class from string


#1

Is there a better way to get to a class when you have the class name in
a string?
(This is rails-related, btw)… For instance, if I have a string clname
= ‘Train’, and I want to access a class method Train#label. I can do
this:

clname = ‘Train’
label = eval “#{clname}.label”

Is there a better way?


#2

From: removed_email_address@domain.invalid [mailto:removed_email_address@domain.invalid] On Behalf
Of Dan K.
Sent: Wednesday, May 31, 2006 2:53 AM

Is there a better way to get to a class when you have the class name in
a string?
(This is rails-related, btw)… For instance, if I have a string clname
= ‘Train’, and I want to access a class method Train#label. I can do
this:

clname = ‘Train’
label = eval “#{clname}.label”

Is there a better way?

Aha.

clname = ‘Train’
label = Object.const_get(clname).new
p label.class #=> Train

V.


#3

On 5/30/06, Dan K. removed_email_address@domain.invalid wrote:

Is there a better way to get to a class when you have the class name in
a string?

This really gets asked a whole lot, especially from Ruby Forum. I
really, really, really wish there was a prominent FAQ on Ruby Forum
with this as like question number 1. This would save people like Dan
from having to ask the question and would save the rest of us from
having to answer it for the 50th time.

Ryan

P.S. No offense Dan, this isn’t your fault.


#4

On May 30, 2006, at 4:52 PM, Dan K. wrote:

label = eval “#{clname}.label”

Is there a better way?


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

In rails you can use #constantize

clname = ‘Train’
clname.constantize.label

But you really should try to search before posting. This was asked a
bunch of times in the last few weeks.

Cheers-
-Ezra


#5

http://www.eachmapinject.com/class_name.html

j`ey
http://www.eachmapinject.com
http://www.eachmapinject.com/class_name.html

$=%q{$=%q{Q};sub(/Q/,$);print};sub(/Q/,$);print


#6

On 5/31/06, Gregory S. removed_email_address@domain.invalid wrote:

The problem with a simple FAQ is that the obvious answer (Object.const_get)
doesn’t handle qualified classes (e.g. Foo::Bar) and the more complicated
version usually given in response only handles fully-qualified classes. I
decided to write a definitive version that handles fully- and
partially-qualified class names, but it isn’t trivial. Honestly, it should
probably be part of Ruby’s core.

Agreed! Has there already been a formal request to add this in 1.9?


#7

On Wed, May 31, 2006 at 09:54:07AM +0900, Ryan L. wrote:
} On 5/30/06, Dan K. removed_email_address@domain.invalid wrote:
} >Is there a better way to get to a class when you have the class name
in
} >a string?
}
} This really gets asked a whole lot, especially from Ruby Forum. I
} really, really, really wish there was a prominent FAQ on Ruby Forum
} with this as like question number 1. This would save people like Dan
} from having to ask the question and would save the rest of us from
} having to answer it for the 50th time.

The problem with a simple FAQ is that the obvious answer
(Object.const_get)
doesn’t handle qualified classes (e.g. Foo::Bar) and the more
complicated
version usually given in response only handles fully-qualified classes.
I
decided to write a definitive version that handles fully- and
partially-qualified class names, but it isn’t trivial. Honestly, it
should
probably be part of Ruby’s core. See
http://redcorundum.blogspot.com/2006/05/kernelqualifiedconstget.html

} Ryan
–Greg


#8

Guest wrote:

Thanks all for the responses – it’s obvious this is a FAQ, but just try
and filter thru all the cruft with the words ‘class’ and ‘string’!! -dan

FWIW, I searched the Ruby forum for

“class from string”

and this rather helpful thread was the 1st one.

(Saved me from asking a FAQ :D)


#9

Thanks all for the responses – it’s obvious this is a FAQ, but just try
and filter thru all the cruft with the words ‘class’ and ‘string’!! -dan


#10

Olie D. wrote:

Guest wrote:

Thanks all for the responses – it’s obvious this is a FAQ, but just try
and filter thru all the cruft with the words ‘class’ and ‘string’!! -dan

FWIW, I searched the Ruby forum for

“class from string”

and this rather helpful thread was the 1st one.

(Saved me from asking a FAQ :D)

Bah! I was too quick on that – now I’ll probably end up asking FAQ’

I’m trying to read-in a folder full of “plug-ins” and call each of them,
in turn. Once I get the class-name, I do something like:

        require "#{PLUG_IN_DIR}/#{one_plugin}"
        plugin_class=Object.const_get(plugin_class_name).new
        plugin_class.some_method

I thought that require worked a bit like the C pre-processor
“include”, in that it would read and execute the named file at that
point, thereby defining my class and its methods. However, when I get
to the middle line, I get

 uninitialized constant PluginClassName

Since rails is mistaking my class-name for a constant, I’m guessing that
require didn’t execute the way I think it does, so my class-name isn’t
initialized.

…Or maybe I’ve completely mis-diagnosed the problem.

At any rate, can someone offer a suggestion for how to read a folder
full of class-definition-files and, once at a time,

  • Execute the class definition, so that my app knows about it
  • Instantiate an instance of the class (I think we have this part,
    above)
  • Call a method on that class (should just be able to say
    “a_class.a_method”, right?)

Thanks!