Proc.arity vs Lambda.arity

后端 未结 3 1035
小蘑菇
小蘑菇 2021-02-19 08:22

Why do proc and lambda return different values for arity?

e.g.

proc   { |x = 0| }.arity       #=> 0
lambda { |a = 0| }.arity       #=> -1
proc   {         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-02-19 09:19

    After reading the other 2 answers, my guess is that in #arity method is treading thin ice. For fixed number of ordered arguments, #arity used to be perfectly OK method. Then, when optional arguments were added, to stick with arity representation by a single integer, minus sign was exploited as a flag. But already, argument field information is being discarded, as eg. 1ary or 2ary -> a, b=1 { a + b } indicates same arity (-2) as -> a, *b { a + b.sum }, taking 1 to arbitrary number of arguments. After the change of #arity behavior in 1.9, another blow comes in 2.0, where named arguments are introduced, which go completely unnoticed by #arity. Again, there will be compulsory and optional named arguments, plus the possibility of collecting arbitrary number of them with hash splash **. I would expect #arity method to change its behavior again in the future...

提交回复
热议问题