开发者

How to determine the number of accepted arguments in a lambda in ruby

开发者 https://www.devze.com 2023-01-06 23:00 出处:网络
Is there any method or som开发者_运维技巧ething x in which, provided z = lambda {|x, y, z| nil} we can say

Is there any method or som开发者_运维技巧ething x in which, provided z = lambda {|x, y, z| nil} we can say

z.x #=> 3

Of course, syntax can differ if it gets the job done. Thank you!


Yes.

z.arity #=> 3


The Proc#arity method tells you this:

ruby-1.9.1-p378 > f = lambda { |x,y,z| nil }
 => #<Proc:0x000001009ca830@(irb):1 (lambda)> 
ruby-1.9.1-p378 > f.arity
 => 3 
0

精彩评论

暂无评论...
验证码 换一张
取 消