Class name as a variable

I

Im still

[Note: parts of this message were removed to make it a legal post.]

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 :)
 
S

spox

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 :)

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"
 
B

Bertram Scharpf

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
 
I

Im still

[Note: parts of this message were removed to make it a legal post.]

2009/8/20 spox said:
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"


==============

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 !
 
I

Im still

[Note: parts of this message were removed to make it a legal post.]

2009/8/20 Bertram Scharpf said:
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


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

=======================
Hi Bertram Scharpf
I think you didn't get me well.
But thank you for the same for replying :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top