Klass.method(:new).arity == -1 violates my POLS!

Hi,

This is not what I expected:

class K
def K.newp(a); end
def initialize(a); end
end

p K.method(:newp).arity #=> 1
p K.method(:new).arity #=> -1

My intuition would be that the knowledge that initialize
takes only 1 param would be known to the new method. I think
this might make sense if you’ve fully grasped Ruby’s dynamic
nature => I haven’t.

Anyone can explain why it has to be like this?

Is there some way to get to know the arity of new/initialize?

Regards,

Karsten

···


http://fastmail.fm - mmm… fastmail…

I don’t know about Ruby v1.6 but in v1.7 with the new allocation
framework you can do this:

K.allocate.method(:initialize).arity #=> 1

The problem is the initialize method is private instance method, so I
don’t know how you can get ahold of it to get its arity without an
instance (which allocate gives you without calling initialize).

-rich

···

-----Original Message-----
From: coma_killen@fastmail.fm [mailto:coma_killen@fastmail.fm]
Sent: Sunday, November 10, 2002 11:50 PM
To: ruby-talk ML
Subject: Klass.method(:new).arity == -1 violates my POLS!

Hi,

This is not what I expected:

class K
def K.newp(a); end
def initialize(a); end
end

p K.method(:newp).arity #=> 1
p K.method(:new).arity #=> -1

My intuition would be that the knowledge that initialize
takes only 1 param would be known to the new method. I think
this might make sense if you’ve fully grasped Ruby’s dynamic
nature => I haven’t.

Anyone can explain why it has to be like this?

Is there some way to get to know the arity of new/initialize?

Regards,

Karsten


http://fastmail.fm - mmm… fastmail…

class K
  def K.newp(a); end
  def initialize(a); end
end

   class L < K
      def self.new(a, b) super(b) end
   end

p K.method(:newp).arity #=> 1
p K.method(:new).arity #=> -1

   p L.method(:new).arity # ==> 2

My intuition would be that the knowledge that initialize
takes only 1 param would be known to the new method.

You really want to have 1 for the arity of L::new ?

Guy Decoux

The problem is the initialize method is private instance method, so I
don't know how you can get ahold of it to get its arity without an
instance (which allocate gives you without calling initialize).

pigeon% cat b.rb
#!/usr/bin/ruby
class K
   def initialize(a) end
end

p K.instance_method(:initialize).arity
pigeon%

pigeon% b.rb
1
pigeon%

Guy Decoux