Class from string

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?

···

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

Of Dan Kirkwood

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.

···

From: list-bounce@example.com [mailto:list-bounce@example.com] On Behalf
Sent: Wednesday, May 31, 2006 2:53 AM

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.

···

On 5/30/06, Dan Kirkwood <dangogh@gmail.com> wrote:

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

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

···

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

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?

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

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

j`ey
http://www.eachmapinject.com <http://www.eachmapinject.com/class_name.html&gt;

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

···

On 5/31/06, Ezra Zygmuntowicz <ezmobius@gmail.com> wrote:

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

> 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?
>
> --
> 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

} >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

···

On Wed, May 31, 2006 at 09:54:07AM +0900, Ryan Leavengood wrote:
} On 5/30/06, Dan Kirkwood <dangogh@gmail.com> 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

···

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

} >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.

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

···

On 5/31/06, Gregory Seidman <gsslist+ruby@anthropohedron.net> wrote:

On Wed, May 31, 2006 at 09:54:07AM +0900, Ryan Leavengood wrote:
} On 5/30/06, Dan Kirkwood <dangogh@gmail.com> wrote:

See Ruby, iOS, and Other Development: Kernel#qualified_const_get

--
R. Mark Volkmann
Object Computing, Inc.

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)

···

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

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'
<sigh>

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!

···

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