Run some method based on some condition

Say I wanted to run a method based on a condition like so:

class Klass
def initialize
a = ‘1st’
b = ‘2nd’
c = ‘3rd’
end

def a
a
end

def b
b
end

def c
c
end
end

ob = Klass.new
[‘a’,‘b’,‘c’].do {|x| ob. here I would want to get at #a and #b and
#c

or maybe better explained like this

var = ‘a’

puts ob.** ??? **

Or if you know perl
$foo = {bar => “baz”};
$baz = “bar”;

puts $foo->{$baz};

—> baz

Sorry to be so confusing.

Thanks,

Ken M.

Farmers Wireless
Office: (256)638-4040 x 224
Mobile: (256)899-3391
Fax: (256)638-2110
[email protected]

CONFIDENTIALITY NOTICE: This e-mail and any attachment to it may contain
confidential and legally privileged information. This information is
intended only for the recipient named above. If you are not the intended
recipient, take notice that any disclosure, copying, distribution or
taking of any action based upon this information is prohibited by law.
If you have received this e-mail in error, please return it to the
sender and delete this copy from your system. Thank you.

Something like this would work:

irb(main):008:0> x = [‘chop!’, ‘chop!’, ‘chop!’]
=> [“chop!”, “chop!”, “chop!”]
irb(main):009:0> y = “This is a string!!!”
=> “This is a string!!!”
irb(main):010:0> x.each {|method| y.send(method)}
=> [“chop!”, “chop!”, “chop!”]
irb(main):011:0> y
=> “This is a string”

The send method allows you to do what you’re asking. :slight_smile:

–Jeremy

On 4/5/07, Ken M. [email protected] wrote:

def a
end
Or if you know perl

Fax: (256)638-2110
this copy from your system. Thank you.


http://www.jeremymcanally.com/

My free Ruby e-book:
http://www.humblelittlerubybook.com/book/

My blogs:

http://www.rubyinpractice.com/

Yes that does exactly what I wanted.

[‘a’,‘b’,‘c’].do {|x| ob.send(x)}

Now I need to do something similar, but probably more difficult.

class Klass

class SubKlass1
def initialize
end
end

class SubKlass2
def initialize
end
end

end

foo = “SubKlass1”

bar = Klass::send(foo).new **That is wrong, because method SubKlass1
doesn’t exist in Klass::Class. But I think you get my point.

On Fri, 2007-04-06 at 07:30 +0900, Jeremy McAnally wrote:

 a = '1st'

end

—> baz

confidential and legally privileged information. This information is

Thanks,

Ken M.

Farmers Wireless
Office: (256)638-4040 x 224
Mobile: (256)899-3391
Fax: (256)638-2110
[email protected]

CONFIDENTIALITY NOTICE: This e-mail and any attachment to it may contain
confidential and legally privileged information. This information is
intended only for the recipient named above. If you are not the intended
recipient, take notice that any disclosure, copying, distribution or
taking of any action based upon this information is prohibited by law.
If you have received this e-mail in error, please return it to the
sender and delete this copy from your system. Thank you.

Have a look at Module#const_get. I think this is what you wanted:

bar = Klass.const_get(foo).new
=> #Klass::SubKlass1:0x2b5a3216e470

Dan