From: Vicente Bosch Campos Date: 2011-03-26T00:19:55+09:00 Subject: Re: Dynamically calling a certain set of methods If I add the method with instance_eval you are correct, but it works if we add them to the class definition with class_eval (hence they get inherited). #! /usr/bin/env ruby require 'ap' class Foo def new_create instance_eval do def xMutation puts "XELLO" end end Foo.class_eval do def yMutation puts "YELLO" end end end end class Bar < Foo end a = Foo.new a.new_create ap a.methods.grep(/Mutation\z/) b = Bar.new ap b.methods.grep(/Mutation\z/) a.xMutation a.yMutation b.yMutation #b.xMutation <- Fails ap Foo.methods.grep(/Mutation\z/) #We have not added any class methods, which is what we wanted In any case I want to do something more dynamic than this and not force the new definitions to the class definition. I am sort of like having a "Library of Congress" singleton class where the new methods created on the fly and the scores obtained with them are stored. Hence when the new objects get created they go into the library and apply to themselves the methods that worked well for past generations ( with a random factor because we want innovation). Its a bit like genetics but with historical memory ? Kind regards, Vicente On Mar 25, 2011, at 10:13 AM, Albert Schlef wrote: > Simon Kaczor wrote in post #989060: >> class Mutating >> ... >> @mutations = instance_methods.grep(/Mutation$/) >> class << self >> attr_reader :mutations >> end > > But if his actual class if derived from Mutating, your code won't work > because the derived class won't see the parent's @mutations. Correct me > if I'm wrong. > > -- > Posted via http://www.ruby-forum.com/. >