From: ptkwt@... (Phil Tomson) Date: 2005-02-11T19:25:01+09:00 Subject: Re: [Newbie] Efficient method lookup from integer In article <_3%Od.11807$8B3.7939@text.news.blueyonder.co.uk>, James wrote: >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