# Using a method as input to another method

**URL:** <https://www.ruby-forum.com/t/using-a-method-as-input-to-another-method/157351>\
**Category:** Ruby\
**Created:** [January 9, 2009, 6:49pm UTC](https://www.ruby-forum.com/t/using-a-method-as-input-to-another-method/157351 "2009-01-09T18:49:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phoenixia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/58956e/32.png) [@phoenixia](https://www.ruby-forum.com/u/phoenixia)\
**Post date:** [January 9, 2009, 6:49pm UTC](https://www.ruby-forum.com/t/using-a-method-as-input-to-another-method/157351/1 "2009-01-09T18:49:04Z")

</div>

Hi, I was wondering if it was possible to do something like the  
following:

class A

def b  
stuff  
end

def c  
stuff  
end

def d(x)  
self.x  
end  
end

Then use thing.d(a) with thing an instance of A?  
Basically, given a long list of methods belonging to a class, if you  
wanted ruby to apply methods based on the state of the program, how  
would you set it up besides doing:

case A  
when b  
thing.stuff  
when c  
thing.otherstuff  
.  
.  
.  
etc.

Thank you for any help with this:)

If ruby can’t do this, are there any languages languages that can treat  
methods as objects?

---

<div class="post-metadata">

**Author:** ![phoenixia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/58956e/32.png) [@phoenixia](https://www.ruby-forum.com/u/phoenixia)\
**Post date:** [January 9, 2009, 7:10pm UTC](https://www.ruby-forum.com/t/using-a-method-as-input-to-another-method/157351/2 "2009-01-09T19:10:33Z")

</div>

On 09.01.2009 18:48, Erick Star wrote:

> stuff  
> end
> 
> def d(x)  
> self.x  
> end  
> end

You can leave out method d and use send() directly, i.e.

a = A.new  
a.send(:b)

However:

> Then use thing.d(a) with thing an instance of A?  
> Basically, given a long list of methods belonging to a class, if you  
> wanted ruby to apply methods based on the state of the program, how  
> would you set it up besides doing:
> 
> case A  
> when b  
> thing.stuff  
> when c  
> thing.otherstuff

State pattern comes to mind.

> **[State pattern](https://en.wikipedia.org/wiki/State_pattern)**
>
> The state pattern is a behavioral software design pattern that allows an object to alter its behavior when its internal state changes. This pattern is close to the concept of finite-state machines. The state pattern can be interpreted as a strategy pattern, which is able to switch a strategy through invocations of methods defined in the pattern's interface.
> The state pattern is used in computer programming to encapsulate varying behavior for the same object, based on its internal state. This can...

There are a number of options for modeling this besides of state  
pattern. Generally any OO approach exploits overriding of methods, i.e.  
you have several classes which share a method with identical signature  
but different behavior.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![phoenixia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/58956e/32.png) [@phoenixia](https://www.ruby-forum.com/u/phoenixia)\
**Post date:** [January 9, 2009, 7:18pm UTC](https://www.ruby-forum.com/t/using-a-method-as-input-to-another-method/157351/3 "2009-01-09T19:18:18Z")

</div>

Thank you for your help,  
This question is part of a larger something I’ve been curious about for  
a while. I, essentially, want to make a set of rules and basic methods,  
then develop a program that can, when given input, determine what  
methods to apply and apply them; and, depending on the rules, if it can  
generate a new method that would give a more desirable outcome.  
Basically, I want to make a program that will have the ability to  
reprogram/build on it self.

Robert K. wrote:

> On 09.01.2009 18:48, Erick Star wrote:
> 
> > stuff  
> > end
> > 
> > def d(x)  
> > self.x  
> > end  
> > end
> 
> You can leave out method d and use send() directly, i.e.
> 
> a = A.new  
> a.send(:b)
> 
> However:
> 
> > Then use thing.d(a) with thing an instance of A?  
> > Basically, given a long list of methods belonging to a class, if you  
> > wanted ruby to apply methods based on the state of the program, how  
> > would you set it up besides doing:
> > 
> > case A  
> > when b  
> > thing.stuff  
> > when c  
> > thing.otherstuff
> 
> State pattern comes to mind.
> 
> [State pattern - Wikipedia](http://en.wikipedia.org/wiki/State_pattern)
> 
> There are a number of options for modeling this besides of state  
> pattern. Generally any OO approach exploits overriding of methods, i.e.  
> you have several classes which share a method with identical signature  
> but different behavior.
> 
> Kind regards
> 
> robert

---

<div class="post-metadata">

**Author:** ![phoenixia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/58956e/32.png) [@phoenixia](https://www.ruby-forum.com/u/phoenixia)\
**Post date:** [January 9, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/using-a-method-as-input-to-another-method/157351/4 "2009-01-09T19:42:48Z")

</div>

Erick Star wrote:

> Thank you for your help,  
> This question is part of a larger something I’ve been curious about for  
> a while. I, essentially, want to make a set of rules and basic methods,  
> then develop a program that can, when given input, determine what  
> methods to apply and apply them; and, depending on the rules, if it can  
> generate a new method that would give a more desirable outcome.  
> Basically, I want to make a program that will have the ability to  
> reprogram/build on it self.

It sounds great but I always remember hearing somewhere that  
self-modifying code was terrible. I cant remember why, maybe it’s the  
debugging thats terribly hard.

Anyway, don’t let me discourage you, it sounds like fun 🙂

Matt

---

<div class="post-metadata">

**Author:** ![phoenixia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/58956e/32.png) [@phoenixia](https://www.ruby-forum.com/u/phoenixia)\
**Post date:** [January 9, 2009, 10:16pm UTC](https://www.ruby-forum.com/t/using-a-method-as-input-to-another-method/157351/5 "2009-01-09T22:16:48Z")

</div>

> Basically, given a long list of methods belonging to a class, if you  
> wanted ruby to apply methods based on the state of the program, how  
> would you set it up besides doing:

I suppose you could use methods like  
next\_method = :method\_name  
instance.send next\_method
