Ruby: define_method vs. def Ruby: define_method vs. def ruby ruby

Ruby: define_method vs. def


define_method is a (private) method of the object Class. You are calling it from an instance. There is no instance method called define_method, so it recurses to your method_missing, this time with :define_method (the name of the missing method), and :screech (the sole argument you passed to define_method).

Try this instead (to define the new method on all Monkey objects):

def method_missing(m)    puts "No #{m}, so I'll make one..."    self.class.send(:define_method, :screech) do      puts "This is the new screech."    endend

Or this (to define it only on the object it is called upon, using the object's "eigenclass"):

def method_missing(m)    puts "No #{m}, so I'll make one..."    class << self      define_method(:screech) do        puts "This is the new screech."      end    endend


self.class.define_method(:screech) doesn't work,because define_method is private methodyou can do that

class << self    public :define_methodenddef method_missing(m)puts "No #{m}, so I'll make one..."Monkey.define_method(:screech) do  puts "This is the new screech."end


def method_missing(m)    self.class.class_exec do       define_method(:screech) {puts "This is the new screech."}    end end

screech method will be available for all Monkey objects.