相关疑难解决方法(0)

为什么带有splat参数的Ruby procs/blocks的行为与方法和lambda不同?

为什么Ruby(2.0)使用splat参数处理/块的行为与方法和lambda不同?

def foo (ids, *args)
  p ids
end
foo([1,2,3]) # => [1, 2, 3]

bar = lambda do |ids, *args|
  p ids
end
bar.call([1,2,3]) # => [1, 2, 3]

baz = proc do |ids, *args|
  p ids
end
baz.call([1,2,3]) # => 1

def qux (ids, *args)
  yield ids, *args
end
qux([1,2,3]) { |ids, *args| p ids } # => 1
Run Code Online (Sandbox Code Playgroud)

以下是对此行为的确认,但没有解释:http: //makandracards.com/makandra/20641-careful-when-calling-a-ruby-block-with-an-array

ruby lambda block splat proc

7
推荐指数
1
解决办法
2117
查看次数

标签 统计

block ×1

lambda ×1

proc ×1

ruby ×1

splat ×1