From: Vicente Bosch Campos Date: 2011-03-23T18:44:33+09:00 Subject: Re: Dynamically calling a certain set of methods Great,much better and concise... (I need to improve on my regexp) Many thanks Robert!! On Mar 23, 2011, at 10:26 AM, Robert Klemme wrote: > On Wed, Mar 23, 2011 at 9:32 AM, Vicente Bosch Campos wrote: >> Hi, >> >> I am doing an implementation of a genetic algorithm to resolve a certain type of equations. I need to randomly call a certain method from a set: >> >> def mutation >> >> self.send(self.methods.select!{|element| element.to_s.end_with?("Mutation")}.shuffle![0]) #I'm using to_s as the symbol object do not allow end_with? >> >> end >> >> def operatorSwitchMutation >> end >> >> def numberSwitchMutation >> end >> >> def unusedNumberMutation >> end >> >> >> Is self.send(self.methods.select!{|element| element.to_s.end_with?("Mutation")}.shuffle![0]) the best way to do it ? Is there a more ruby idiom to perform the safe thing ? > > 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/ >