Y
Yossef Mendelssohn
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' }
=> #<Proc:0x0000ec54@(irb):1>
irb(main):002:0> x.call
=> "hi"
irb(main):003:0> x.call(5)
=> "hi"
irb(main):004:0> x = lambda { |arg| p arg }
=> #<Proc:0x000843a0@(irb):4>
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' }
=> #<Proc:0x00067674@(irb):8>
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?
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' }
=> #<Proc:0x0000ec54@(irb):1>
irb(main):002:0> x.call
=> "hi"
irb(main):003:0> x.call(5)
=> "hi"
irb(main):004:0> x = lambda { |arg| p arg }
=> #<Proc:0x000843a0@(irb):4>
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' }
=> #<Proc:0x00067674@(irb):8>
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?