Ruby: define_method vs. def

前端 未结 3 578
梦毁少年i
梦毁少年i 2020-12-07 08:21

As a programming exercise, I\'ve written a Ruby snippet that creates a class, instantiates two objects from that class, monkeypatches one object, and relies on method_missin

3条回答
  •  误落风尘
    2020-12-07 08:59

    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."
        end
    end
    

    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
        end
    end
    

提交回复
热议问题