arity of methods defined with define_method

P

Paul Brannan

Why do I get the following arities:

irb(main):005:0> [pbrannan@zaphod tmp]$ irb
irb(main):001:0> RUBY_VERSION
=> "1.6.8"
irb(main):002:0> class Foo; define_method :foo, proc {}; end
=> #<Proc:0x402629e8>
irb(main):003:0> Foo.instance_method:)foo).arity
=> -135401849
irb(main):004:0>

[pbrannan@zaphod tmp]$ irb-1.8
irb(main):001:0> RUBY_VERSION
=> "1.8.1"
irb(main):002:0> class Foo; define_method :foo, proc {}; end
=> #<Proc:0x00000000@(irb):2>
irb(main):003:0> Foo.instance_method:)foo).arity
=> 1

In the first case I get a completely unexpected arity; I suspect this is
a bug that was fixed in 1.8?

In the second I get an arity of 1, but I can clearly call the method
with 0 or 3 arguments:

irb(main):004:0> Foo.new.foo()
=> nil
irb(main):005:0> Foo.new.foo(1, 2, 3)
=> nil

I would expect that a method that can take any number of arguments would
have an arity of -1. Why is this not the case with methods defined with
define_method()?

Paul
 
N

nobu.nokada

Hi,

At Thu, 27 Nov 2003 23:52:08 +0900,
Paul said:
I would expect that a method that can take any number of arguments would
have an arity of -1. Why is this not the case with methods defined with
define_method()?

It's a bug.

* eval.c (method_arity): used wrong Proc object. [ruby-talk:86504]


Index: eval.c
===================================================================
RCS file: /cvs/ruby/src/ruby/eval.c,v
retrieving revision 1.597
diff -u -2 -p -r1.597 eval.c
--- eval.c 27 Nov 2003 15:47:33 -0000 1.597
+++ eval.c 27 Nov 2003 16:06:46 -0000
@@ -7602,5 +7602,5 @@ method_arity(method)
case NODE_BMETHOD:
case NODE_DMETHOD:
- return proc_arity(method);
+ return proc_arity(body->nd_cval);
default:
body = body->nd_next; /* skip NODE_SCOPE */
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: arity of methods defined with define_method"

|> I would expect that a method that can take any number of arguments would
|> have an arity of -1. Why is this not the case with methods defined with
|> define_method()?
|
|It's a bug.
|
|* eval.c (method_arity): used wrong Proc object. [ruby-talk:86504]

Please commit.

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top