Why do Ruby (2.0) procs/blocks with splat arguments behave differently than methods and lambdas?
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
Here's a confirmation of this behavior, but without explanation: http://makandracards.com/makandra/20641-careful-when-calling-a-ruby-block-with-an-array
join...
is only making it unnecessarily complicated. It is irrelevant to your question. All you should do is dop ids
within each block, and make it clear how it differs. – Lukashproc
being a standard library method whilelambda
being a special keyword... – Vulpine*args
makes the arity optional, but the splat is still applied. I suspect it might be a bug. – Lukashf = ->((x, *xs)) { ... }
, so non-lambda block can probably be considered as lambda with implicit parenthesis. – Damascenelambda
andproc
are both method:[3] pry(main)> method :lambda => #<Method: Object(Kernel)#lambda>
I have checked it at Ruby 1.9.3 and 2.0.0. – Warilylambda
is also a keyword. When you use it directly, returning from the lambda's body does not return from the enclosing method, but when you use it viamethod(:lambda).call
it behaves like a regular method-with-block and returning from the block returns from the enclosing method. – Vulpinelambda
/->
returns from block BUTproc
/Proc.new
raisesLocalJumpError: unexpected return
. It is indeed weird, because in the mentioned versionslambda
works as I described. It might be 2.1.* bug. – Warily