"James" <az / despammed.com> schrieb im Newsbeitrag
news:_3%Od.11807$8B3.7939 / text.news.blueyonder.co.uk...
> Hi,
>
> I've done much OO coding in perl 5 and I'm
> making the move over to ruby.
>
> Gosh, don't your programs get shorter!
>
> What would be a good ruby idiom to move efficiently from
> a positive integer in the range 0 to ~2000 to a specific
> method call?
>
> A case / when statement would do this simply, but in perl
> I sped this up by building a lookup table using
> an array containing references to the appropriate perl subs.
>
> Best Wishes,
> James
>
> --
> "Be excellent to each other", Bill & Ted

class Foo
  METHODS = {1=>:foo, 2=>:bar}

  def invoke(id, *a,&b)
    send(METHODS[id],*a,&b)
  end

  def foo() "foo" end
  def bar() "bar" end
end

>> Foo.new.invoke 1
=> "foo"
>> Foo.new.invoke 2
=> "bar"

class Foo
  def initialize
    @methods = {
      1 => lambda {"foo"},
      2 => lambda {"bar"},
    }
  end

  def invoke(id, *a)
    @methods[id].call(*a)
  end
end

>> Foo.new.invoke 1
=> "foo"
>> Foo.new.invoke 2
=> "bar"

Of course you can use an Array instead of the Hash.

Kind regards

    robert