Search code examples
rubyclosuresmetaprogramming

How do I declare a method dynamically with method_missing?


I have a ruby program, and I want to accept the user's made up method, and make a new method out of that name. I have tried this:

def method_missing(meth,*args,&block)
  name = meth.to_s
  class << self
    define_method(name) do
      puts "hello " + name
    end
  end
end

And I get the following error:

`define_method': interning empty string (ArgumentError) in 'method_missing'

Any ideas? Thanks.

Edit:

I got it working a different way, but I'm still curious how to do it this way. Here is my code:

def method_missing(meth,*args,&block)
  Adder.class_eval do
    define_method(meth) do
      puts "hello " + meth
    end
  end
  send("#{meth}")
end

Solution

  • The variable name is not available inside the class definition (class << self) scope. It isn't throwing a NameError because you've overridden method_missing.

    To do what you're trying to do, you need to keep the scope with name. In order to do that, you have to only use block-based methods (e.g. class_eval) instead of directly opening the class, so something like this:

    def method_missing(meth,*args,&block)
      name = meth.to_s
      eigenclass = class << self; self; end
      eigenclass.class_eval do
        define_method(name) do
          puts "hello " + name
        end
      end
    end
    

    But actually, the symbol in meth is quite sufficient — you don't need name at all. (Though you'll still need the above technique either way.) On top of that, though, you want to execute the method immediately. The simplest way would just be to resend the message:

    def method_missing(meth,*args,&block)
      eigenclass = class << self; self; end
      eigenclass.class_eval do
        define_method(meth) do
          puts "hello #{meth}"
        end
      end
      send(meth, *args, &block)
    end