From: Vicente Bosch Campos Date: 2011-03-25T06:56:10+09:00 Subject: Re: Dynamically calling a certain set of methods I am actually modifying the mutation methods and creating more on the fly .... loving metaprogramming capabilities in Ruby to code genetic algorithms: hence I think its best to not precompile. In any case a great idea if I do something a bit less random. Thanks Simon. On Mar 24, 2011, at 8:20 PM, Simon Kaczor wrote: > You can save yourself some CPU cycles by pre-compiling the list of > available methods as a class instance variable: > > class Mutating > ... > @mutations = instance_methods.grep(/Mutation$/) > class << self > attr_reader :mutations > end > > def mutation > send(self.class.mutations.sample) > end > end > > Simon > >>> First of all you can save typing by removing "self.". Then you should >>> replace #shuffle![0] with #sample: >>> >>> send(methods.select!{|element| element.to_s.end_with?("Mutation")}.sample) >>> >>> If you like you can strip this down even further by using regular >>> expressions because those happen to work with Symbols as well: >>> >>> send(methods.grep(/Mutation\z/).sample) >>> >>> :-) >>> >>> Kind regards >>> >>> robert >>> >>> -- >>> remember.guy do |as, often| as.you_can - without end >>> http://blog.rubybestpractices.com/ >>> >> >> > >