From: Sam Uel Date: 2010-05-18T22:13:36+09:00 Subject: Re: Dynamically adding methods to a class Thanks very much guys! I'm still confused about this one thing, though. Example: class Fred def method_missing name, *args puts "inside method_missing" Fred.class_eval "def #{name} val; puts val; end" send(name, args) end end fr=Fred.new fr.somefunc "1st time - called by the var fr" fr.somefunc "2nd time - called by the var fr" Fred.new.somefunc "3rd time, this one's a new instance" The code above gives the following output: >>inside method_missing >>1st time - called by the var fr >>2nd time - called by the var fr >>3rd time, this one's a new instance ^^ Notice how the message "inside method_missing" only shows up once. Everything is OK so far, the method somefunc seems to have correctly added itself as an instance method to the class Fred.. but the following confuses me: puts fr.methods.include? "somefunc" puts Fred.new.methods.include? "somefunc" puts Fred.instance_methods.include? "somefunc" Gives the output: >>false >>false >>false How is it that none of these can find the method somefunc? Could anyone else try this out? I've attached the code Attachments: http://www.ruby-forum.com/attachment/4740/Test.rb -- Posted via http://www.ruby-forum.com/.