How does ruby unpack arguments passed into Proc?

Viewed 1635
a_proc = Proc.new {|a,b,*c| p c; c.collect {|i| i*b }}
puts a_proc[2,2,4,3]

Code above is pretty intuitive according to https://ruby-doc.org/core-2.2.0/Proc.html, a_proc[2,2,4,3] is just a syntax sugar for a_proc.call(2,2,4,3) to hide “call”

But the following (works well) confused me a lot

a=[2,2,4,3]
puts a_proc.call(a)
puts a_proc.call(*a)

It seems very different from a normal function call, cause it doesn't check the number arguments passed in.

However, as expected the method calling semantics will raise an error if using parameters likewise

def foo(a,b,*c)
  c.collect{|i| i*b}
end
foo([1,2,3,4]) #`block in <main>': wrong number of arguments (given 1, expected 2+) (ArgumentError)

foo(*[1,2,3,4]) #works as expected

I do not think such an inconsistency as a design glitch, so any insights on this will be appreciated.

2 Answers
Related