On 8/23/06, Jan Svitok <jan.svitok / gmail.com> wrote:
> On 8/23/06, Dikshit Saumya-G19445 <somc / motorola.com> wrote:
> > Hi
> > I am a newbie to this mailing list.
> >
> > I have a couple of queries:> >
> > 1. Can methods be part of an array definition, which can bear similarity
> > to 'C' definitions
> >    of "array of function pointers", as part of a state machine
> > implementation.
>
> Look up Object#method and Method in the docs:
>
> you can do ['aaa'.method(:size), 10.method("+")]
>
> UnboundMethod could be useful as well.
>
> Other than that, its possible to call methods by name. I you just need
> to call methods on one object, it is enough to store just their names
> (preferably as symbols):
>
> methods = [:size, :length, :whatever]
>
> then you would for example do: methods.each {|m| 'aaaa'.send(m) }
>
>


Another approach would be to use Procs in the array instead of
methods, this would allow you to store arguments in the array entries
as well, and to execute more than just a method.

actions = [
  lambda {obj.size},
  lambda {obj.length},
  lambda {obj.foo(3, "abc") }
  lambda {puts "Hi Mom!"; obj.callhome }
]

then to invoke an action it's just

actions[n].call

as a variation on this, if the blocks have parameters, you can use
call(arg1, [arg2,...]) to pass parameters.

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/