Ruby: define_method vs. def

前端 未结 3 572
梦毁少年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:52

    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.

提交回复
热议问题