[Newbie] Efficient method lookup from integer

J

James

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
 
R

Robert Klemme

James said:
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

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
=> "bar"

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

def invoke(id, *a)
@methods[id].call(*a)
end
end
=> "bar"

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

Kind regards

robert
 
P

Phil Tomson

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
 
G

gabriele renzi

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 :)
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"
 
J

James

gabriele said:
James ha scritto:
...
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
 
M

Mike Woodhouse

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top