# Run some method based on some condition

**URL:** <https://www.ruby-forum.com/t/run-some-method-based-on-some-condition/91253>\
**Category:** Ruby\
**Created:** [April 6, 2007, 12:18am UTC](https://www.ruby-forum.com/t/run-some-method-based-on-some-condition/91253 "2007-04-06T00:18:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kennyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d2c977/32.png) [@kennyd](https://www.ruby-forum.com/u/kennyd)\
**Post date:** [April 6, 2007, 12:18am UTC](https://www.ruby-forum.com/t/run-some-method-based-on-some-condition/91253/1 "2007-04-06T00:18:48Z")

</div>

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  
removed\_email\_address@domain.invalid

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.

---

<div class="post-metadata">

**Author:** ![kennyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d2c977/32.png) [@kennyd](https://www.ruby-forum.com/u/kennyd)\
**Post date:** [April 6, 2007, 12:31am UTC](https://www.ruby-forum.com/t/run-some-method-based-on-some-condition/91253/2 "2007-04-06T00:31:48Z")

</div>

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. 🙂

–Jeremy

On 4/5/07, Ken M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def a  
> end  
> Or if you know perl
> 
> Fax: (256)638-2110  
> this copy from your system. Thank you.

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![kennyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d2c977/32.png) [@kennyd](https://www.ruby-forum.com/u/kennyd)\
**Post date:** [April 6, 2007, 12:36am UTC](https://www.ruby-forum.com/t/run-some-method-based-on-some-condition/91253/3 "2007-04-06T00:36:50Z")

</div>

Yes that does exactly what I wanted.

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

---

<div class="post-metadata">

**Author:** ![kennyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d2c977/32.png) [@kennyd](https://www.ruby-forum.com/u/kennyd)\
**Post date:** [April 6, 2007, 1:49am UTC](https://www.ruby-forum.com/t/run-some-method-based-on-some-condition/91253/4 "2007-04-06T01:49:57Z")

</div>

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  
removed\_email\_address@domain.invalid

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.

---

<div class="post-metadata">

**Author:** ![kennyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d2c977/32.png) [@kennyd](https://www.ruby-forum.com/u/kennyd)\
**Post date:** [April 6, 2007, 3:55am UTC](https://www.ruby-forum.com/t/run-some-method-based-on-some-condition/91253/5 "2007-04-06T03:55:14Z")

</div>

Have a look at Module#const\_get. I think this is what you wanted:

> > bar = Klass.const\_get(foo).new  
> > =\> #Klass::SubKlass1:0x2b5a3216e470

Dan
