# Class name as a variable

**URL:** <https://www.ruby-forum.com/t/class-name-as-a-variable/174036>\
**Category:** Ruby\
**Created:** [August 19, 2009, 9:08pm UTC](https://www.ruby-forum.com/t/class-name-as-a-variable/174036 "2009-08-19T21:08:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 19, 2009, 9:08pm UTC](https://www.ruby-forum.com/t/class-name-as-a-variable/174036/1 "2009-08-19T21:08:03Z")

</div>

Hi all

I’m thinking about a question, that is : Several classes will have a  
method  
in a same name (probably do same thing too).  
then in a program I want the method calling class name coming from a  
variable( or maybe a result of a expr).

for example:

```
class A
  def abc end
end

class B
  def abc end
end

#what i want to do is something like

a = a_variable.abc # a_variable = A | B

```

Now I’m doing this by

a = eval(" #{a\_variable}.abc ")

My question is, is there any better (or nicer) way to doing this?

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 19, 2009, 9:27pm UTC](https://www.ruby-forum.com/t/class-name-as-a-variable/174036/2 "2009-08-19T21:27:21Z")

</div>

On Wednesday 19 August 2009 12:07:06 pm Im still wrote:

> ```
> def abc end
> 
> ```
> 
> Now I’m doing this by
> 
> a = eval(" #{a\_variable}.abc ")
> 
> My question is, is there any better (or nicer) way to doing this?
> 
> Thanks 🙂

With how you asked your question, I’m assuming you want to call a static  
method. Otherwise, what’s the point of the eval. So, is something like  
this  
what you are after:

class A  
def A.abc  
“fu”  
end  
end  
class B  
def B.abc  
“bar”  
end  
end

With this, there are a couple ways to do this. One, have your variable  
hold  
the constant:

var = A  
var.abc -\> “fu”  
var = B  
var.abc -\> “bar”

If the variable holds the class name as a string, or symbol, you can  
fetch the  
constant:

var = ‘A’  
Kernel.const\_get(var).abc -\> “fu”  
var = :B  
Kernel.const\_get(var).abc -\> “bar”

---

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 20, 2009, 4:20am UTC](https://www.ruby-forum.com/t/class-name-as-a-variable/174036/3 "2009-08-20T04:20:02Z")

</div>

Hi,

Am Donnerstag, 20. Aug 2009, 04:07:06 +0900 schrieb Im still:

> a = eval(" #{a\_variable}.abc ")
> 
> My question is, is there any better (or nicer) way to doing this?

[untested]

What you are doing is

a = eval a\_variable.to\_s.abc.to\_s

You probably meant

a = eval a\_variable.abc.to\_s

or even something else.

Bertram

---

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 20, 2009, 5:08am UTC](https://www.ruby-forum.com/t/class-name-as-a-variable/174036/4 "2009-08-20T05:08:39Z")

</div>

2009/8/20 spox [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > ```
> > a = a_variable.abc # a_variable = A | B
> > 
> > ```
> 
> method. Otherwise, what’s the point of the eval. So, is something like this  
> end  
> If the variable holds the class name as a string, or symbol, you can fetch  
> the  
> constant:
> 
> var = ‘A’  
> Kernel.const\_get(var).abc → “fu”  
> var = :B  
> Kernel.const\_get(var).abc → “bar”
> 
> ==============

Hi Spox  
Ye, they should be static methods, else it will be wrong. My mistake  
:DThanks  
very much, what you told is exactly what I want !

---

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 20, 2009, 5:12am UTC](https://www.ruby-forum.com/t/class-name-as-a-variable/174036/5 "2009-08-20T05:12:45Z")

</div>

2009/8/20 Bertram S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> What you are doing is
> 
> –  
> Bertram S.  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)
> 
> =======================  
> Hi Bertram S.  
> I think you didn’t get me well.  
> But thank you for the same for replying 🙂
