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.
"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.
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:
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"