Forum: Ruby Newbie: Reflection question(?)

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.
A393d0639e9c313bcee804a12303b9e5?d=identicon&s=25 Gaudi Mi (gaudimila)
on 2006-03-17 05:55
At runtime I have the name of a class in a string, and I want to call a
method on the class having that name. How do I do that?

E.g., let's say that we have several classes that all have a method
called address().
At run-time I know I want to call the method address() on a particular
class, and that class name is in a string.

Thanks.
G
2a0f7bd2c54fbc44329d69555b96f1c5?d=identicon&s=25 Kev Jackson (Guest)
on 2006-03-17 06:03
(Received via mailing list)
Gaudi Mi wrote:

>At runtime I have the name of a class in a string, and I want to call a
>method on the class having that name. How do I do that?
>
>E.g., let's say that we have several classes that all have a method
>called address().
>At run-time I know I want to call the method address() on a particular
>class, and that class name is in a string.
>
>
you need to look at call

eg to instantiate an object (call Object.new)

m = c.method(:new)
o = m.call

to call address method

m = c.method(:address)
m.call

Kev
A393d0639e9c313bcee804a12303b9e5?d=identicon&s=25 Gaudi Mi (gaudimila)
on 2006-03-17 06:10
I'll try that, thanks Kev!

G


Kev Jackson wrote:
> Gaudi Mi wrote:
>
>>At runtime I have the name of a class in a string, and I want to call a
>>method on the class having that name. How do I do that?
>>
>>E.g., let's say that we have several classes that all have a method
>>called address().
>>At run-time I know I want to call the method address() on a particular
>>class, and that class name is in a string.
>>
>>
> you need to look at call
>
> eg to instantiate an object (call Object.new)
>
> m = c.method(:new)
> o = m.call
>
> to call address method
>
> m = c.method(:address)
> m.call
>
> Kev
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-03-17 08:48
(Received via mailing list)
On Fri, 2006-03-17 at 13:55 +0900, Gaudi Mi wrote:
> At runtime I have the name of a class in a string, and I want to call a
> method on the class having that name. How do I do that?

Here are two possible ways:

str = "Array"
# => "Array"

ary_clz = eval(str)
# => Array

ary_clz.class
# => Class

ary_clz.new
# => []

##################

ary_clz = Object.const_get(str)
# => Array

ary_clz.new
# => []
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-03-17 12:09
(Received via mailing list)
"Ross Bamford" <rossrt@roscopeco.co.uk> wrote in message
news:1142581508.1295.3.camel@jukebox.roscopeco...
> # => Array
> # => Array
>
> ary_clz.new
> # => []

const_get is definitely preferred as it doesn't show the same security
risks as eval does.

Addtional note, for nested class names:

name.split(/::/).inject(Object) {|cl,n| cl.const_get(n)}.address()

Kind regards

    robert
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 Benjohn Barnes (Guest)
on 2006-03-18 01:47
(Received via mailing list)
On 17 Mar 2006, at 05:01, Kev Jackson wrote:

> you need to look at call
>
> eg to instantiate an object (call Object.new)
>
> m = c.method(:new)
> o = m.call

I don't think that's going to work for him (if c is the
name_of_class_string that he's got). He needs to look up the class
named name_of_class_string:

	ObjectSpace.const_get(name_of_class_string).new(*any_arguments)

I'd like to check that is the "right way" to do it, but I don't have
any reference books to hand.
This topic is locked and can not be replied to.