From: Yossef Mendelssohn Date: 2007-10-09T23:33:52+09:00 Subject: Using define_method isn't the same as defining a method I apologize if this has been brought up before. A cursory search through the archives didn't come up with anything that addressed this same subject. I know that creating a Proc (at least a lambda-style Proc) will do argument checking upon call, and I realize (even though I don't like it) that those arguments are handled a little oddly: irb(main):001:0> x = lambda { 'hi' } => # irb(main):002:0> x.call => "hi" irb(main):003:0> x.call(5) => "hi" irb(main):004:0> x = lambda { |arg| p arg } => # irb(main):005:0> x.call (irb):4: warning: multiple values for a block parameter (0 for 1) from (irb):5 nil => nil irb(main):006:0> x.call(5) 5 => nil irb(main):007:0> x.call(5,6) (irb):4: warning: multiple values for a block parameter (2 for 1) from (irb):7 [5, 6] => nil irb(main):008:0> x = lambda { |arg1, arg2| 'hello' } => # irb(main):009:0> x.call ArgumentError: wrong number of arguments (0 for 2) from (irb):9 from (irb):9:in `call' from (irb):9 from :0 irb(main):010:0> x.call(5) ArgumentError: wrong number of arguments (1 for 2) from (irb):8 from (irb):10:in `call' from (irb):10 from :0 irb(main):011:0> x.call(5,6) => "hello" irb(main):012:0> x.call(5,6,7) ArgumentError: wrong number of arguments (3 for 2) from (irb):8 from (irb):12:in `call' from (irb):12 from :0 What I really don't like is how define_method, since it takes a block, uses this same sort of argument handling. That means define_method :meth do |arg| end is the equivalent of def meth(*arg) end with a warning. If I want to create a method that takes one argument and acts like a normal method, what are my choices? Apparently, I get to use only 'def' itself, but that means I need string eval if the method name is contained in a variable. Am I wrong? Is there a better way? -- -yossef