Ruby: define_method vs. def

前端 未结 3 565
梦毁少年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.

    0 讨论(0)
  • 2020-12-07 08:55

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

    class << self
        public :define_method
    end
    def method_missing(m)
    puts "No #{m}, so I'll make one..."
    Monkey.define_method(:screech) do
      puts "This is the new screech."
    end
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题