[Newbie] Efficient method lookup from integer

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

"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

In article <_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.

You could essentially do the same thing with some modifications. First,
you could build an array of symbols which are names of the method you want
to call, something like:

  methods =

  methods[0] = :foo
  methods[1] = :bar

.... etc.

Then you could put all of your methods into a class:

  class Methods
    def foo
      "foo" #foo behavior
    end
    def bar
      "bar" #bar behavior
    end
  end

Then you should be able to do:

  m = Methods.new
  m.method(methods[0]).call

Or, instead of storing symbols in your methods array , you could store
Method objects and then just call them like so:

  methods[0].call

Phil

···

James <az@despammed.com> wrote:

James ha scritto:

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.

people already gave you good answers, so I am her with a question :slight_smile:
Why do you want that?

Supposing you have 2000 autogenerated methods, you could put them in a module named like:
method_xxxx
and just call them with

module.send "method_#{number}"

or something like that, and avoid the intermediary array/hash, but it seem strange to me to have all that method named as "numbers"

How different are the methods likely to be? Are there really going to
be 2000 of them? I can't imagine that there won't be some commonality.

Could

method_missing?

be useful here? Not asserting a better solution, just mentioning
something that came to mind...

--Mike

gabriele renzi wrote:

James ha scritto:

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?

...
Why do you want that?

... just call them with

module.send "method_#{number}"

The simple answer is, I am still learning ruby and did not know
about either module.send or the .call idiom used in some other answers.

[Newbie] as a subject prefix is intended to show myself as a beginner.

I am grateful to everyone who has helped me so quickly.

Thanks!

Best Wishes,
James

···

--
"Be excellent to each other", Bill & Ted