# Newbie: Reflection question(?)

**URL:** <https://www.ruby-forum.com/t/newbie-reflection-question/55129>\
**Category:** Ruby\
**Created:** [March 17, 2006, 5:55am UTC](https://www.ruby-forum.com/t/newbie-reflection-question/55129 "2006-03-17T05:55:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 17, 2006, 5:55am UTC](https://www.ruby-forum.com/t/newbie-reflection-question/55129/1 "2006-03-17T05:55:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 17, 2006, 6:03am UTC](https://www.ruby-forum.com/t/newbie-reflection-question/55129/2 "2006-03-17T06:03:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 17, 2006, 6:10am UTC](https://www.ruby-forum.com/t/newbie-reflection-question/55129/3 "2006-03-17T06:10:49Z")

</div>

I’ll try that, thanks Kev!

G

Kev J. 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

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 17, 2006, 8:48am UTC](https://www.ruby-forum.com/t/newbie-reflection-question/55129/4 "2006-03-17T08:48:17Z")

</div>

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

# =\> []

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 17, 2006, 12:09pm UTC](https://www.ruby-forum.com/t/newbie-reflection-question/55129/5 "2006-03-17T12:09:55Z")

</div>

“Ross B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> # =\> 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

```

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 18, 2006, 1:47am UTC](https://www.ruby-forum.com/t/newbie-reflection-question/55129/6 "2006-03-18T01:47:18Z")

</div>

On 17 Mar 2006, at 05:01, Kev J. 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.
