在Eloquent Ruby中有一个我不明白的代码示例.
class Document
attr_accessor :save_listener
# most of the class omitted...
def on_save( &block )
@save_listener = block
end
def save( path )
File.open( path, 'w' ) { |f| f.print( @contents ) }
@save_listener.call( self, path ) if @save_listener
end
end
# usage
my_doc = Document.new( 'block based example', 'russ', '' )
my_doc.on_save do |doc|
puts "Hey, I've been saved!"
end
Run Code Online (Sandbox Code Playgroud)
为什么@save_listener.call( self, path )
需要两个参数?保存的块看起来只有一个参数|doc|
.这是书中的拼写错误还是我在这里缺少什么?
我甚至尝试输入此代码并执行它,我发现我可以添加任意数量的参数,并且不会出现任何错误.但我仍然不明白为什么在这个例子中有两个参数.
这是由于Proc
和之间的细微差别Lambda
.使用Proc
代码块创建新代码时,可以在调用时传递任意数量的参数.例如:
proc = Proc.new {|a,b| a + b}
proc.arity #=> 2 -- the number of arguments the Proc expects
proc.call(4,8) #=> 12
proc.call(4,8,15,16,23,42) #=> 12
Run Code Online (Sandbox Code Playgroud)
它接受了这些参数,但没有将它们分配给块中的任何变量.
但是,Lambda
关心论点的数量.
proc = lambda {|a,b| a + b}
proc.arity #=> 2
proc.call(4,8) #=> 12
proc.call(4,8,15,16,23,42) #=> ArgumentError: wrong number of arguments (6 for 2)
Run Code Online (Sandbox Code Playgroud)
这样做的原因是因为Proc.call
赋予方法的参数类似于变量的并行赋值.
num1, num2 = 1,2 #=> num1 is 1, num2 is 2
num1, num2 = 1 #=> num1 is 1, num2 is nil
num1, num2 = 1,2,3,4,5 #=> num1 is 1, num2 is 2 and the rest are discarded
Run Code Online (Sandbox Code Playgroud)
但是,Lambda
这样不起作用.Lambda
更像是方法调用而不是变量赋值.
因此,如果您担心只允许一定数量的参数,请使用Lambda
.但是,在此示例中,由于您可以添加块的路径,Proc
因此最好.