Forum: Ruby class from string

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
89b03275ec88784f70165ca283894e12?d=identicon&s=25 Dan Kirkwood (Guest)
on 2006-05-31 01:52
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?
2c7c807a1df0c76a8fc823c709b501a9?d=identicon&s=25 Victor Shepelev (Guest)
on 2006-05-31 02:07
(Received via mailing list)
From: list-bounce@example.com [mailto:list-bounce@example.com] On Behalf
Of Dan Kirkwood
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.
4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 Ryan Leavengood (Guest)
on 2006-05-31 02:56
(Received via mailing list)
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?

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.
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-05-31 03:42
(Received via mailing list)
On May 30, 2006, at 4:52 PM, Dan Kirkwood 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
5d15f83f0e681b138d95f1ec430868fa?d=identicon&s=25 Joey (Guest)
on 2006-05-31 10:16
(Received via mailing list)
Bf6862e2a409078e13a3979c00bba1d6?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-05-31 15:16
(Received via mailing list)
On Wed, May 31, 2006 at 09:54:07AM +0900, Ryan Leavengood wrote:
} 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?
}
} 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/kernelqual...

} Ryan
--Greg
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-05-31 15:44
(Received via mailing list)
On 5/31/06, Gregory Seidman <gsslist+ruby@anthropohedron.net> 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?
89b03275ec88784f70165ca283894e12?d=identicon&s=25 Guest (Guest)
on 2006-05-31 17:37
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
36a1e646d764b4bd1e87a312aea67c4c?d=identicon&s=25 Olie D. (olie)
on 2007-11-04 03:38
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)
36a1e646d764b4bd1e87a312aea67c4c?d=identicon&s=25 Olie D. (olie)
on 2007-11-04 18:27
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!
This topic is locked and can not be replied to.