Forum: Ruby Using a method as input to another method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D3164b5f7fe45033ebbfd7548af09ccf?d=identicon&s=25 Erick Star (phoenixia)
on 2009-01-09 18:49
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?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-09 19:10
(Received via mailing list)
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.

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
D3164b5f7fe45033ebbfd7548af09ccf?d=identicon&s=25 Erick Star (phoenixia)
on 2009-01-09 19:18
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 Klemme 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.
>
> 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
8fa0f5c2bf8ad238baa4e20cb0360ebd?d=identicon&s=25 Matt Harrison (iwasinnamuknow)
on 2009-01-09 19:42
(Received via mailing list)
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
Ab0b39142721bf4ab6492bdeb18c9682?d=identicon&s=25 Roger Pack (Guest)
on 2009-01-09 22:16
(Received via mailing list)
>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
This topic is locked and can not be replied to.