When there are no optional parameters (and zero or more required
parameters), all three objects report the same arity. This is what
you’d expect in a normal situation.
With an optional/default parameter, the Proc object reports number of
“required” parameters*, while the Method and Lambda objects report the
one’s complement.
With an optional/default parameter, the Proc object reports number of
“required” parameters*, while the Method and Lambda objects report the
one’s complement.
l = lambda { |a,*b,c| }
=> #<Proc:0x84bbeac@(pry):2 (lambda)>
l.arity
=> -3
p.arity
=> -3
def m(a,*b,c);end
=> nil
method(:m).arity
=> -3
I don’t know that that’s an exception so much as an inconsistency in
Proc. But again, I don’t know why Proc reports its arity the way it
does in the first place.
It makes sense for the Lambda, because you need to provide values for a
and c, so there are two mandatory parameters. ~2 = -3
l = lambda{|a,*b,c| p a, b, c }
l[1,2] #=> 1, [], 2
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.