Project

General

Profile

Actions

Bug #6476

closed

Proc unrolls an array even without splat

Added by prijutme4ty (Ilya Vorontsov) almost 12 years ago. Updated almost 12 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
1.9.3p194
Backport:
[ruby-core:45176]

Description

Normal behaviour:

def pr_def(recv,args)
puts "#{recv.inspect} + #{args.inspect}"
end
pr_def(
[1,2,3]) # => [1] + [2,3]
pr_def([1,2,3]) # => [1,2,3] + []
pr_def([[1,2,3]]) # => [[1,2,3]] + []

pr_lambda = lambda{|recv,args| puts "#{recv.inspect} + #{args.inspect}"}
pr_lambda.call(
[1,2,3]) # => [1] + [2,3]
pr_lambda.call([1,2,3]) # => [1,2,3] + []
pr_lambda.call([[1,2,3]]) # => [[1,2,3]] + []

But Proc acts in a different way.

pr_proc = Proc.new{|recv,args| puts "#{recv.inspect} + #{args.inspect}"}
pr_proc.call(
[1,2,3]) # => 1 + [2,3]
pr_proc.call([1,2,3]) # => 1 + [2,3]
pr_proc.call([[1,2,3]]) # => [1,2,3] + []

Is it right behaviour or such unsplatting is a bug?

Updated by mame (Yusuke Endoh) almost 12 years ago

  • Status changed from Open to Rejected

Hello,

prijutme4ty (Ilya Vorontsov) wrote:

pr_def(*[1,2,3]) # => [1] + [2,3]

It should be: "1 + [2, 3]"

Is it right behaviour or such unsplatting is a bug?

It is the spec. A method and lambda use a strict rule for arguments,
but a proc uses a flexible one: it does not distinguish multiple
arguments from one array argument.

def foo
yield 1, 2
end
def bar
yield [1, 2]
end

f = proc {|x, y| p x + y }
foo(&f) #=> 3
foo(&f) #=> 3

f.call(1, 2) #=> 3
f.call([1, 2]) #=> 3

--
Yusuke Endoh

Updated by ko1 (Koichi Sasada) almost 12 years ago

(2012/05/23 0:23), mame (Yusuke Endoh) wrote:

prijutme4ty (Ilya Vorontsov) wrote:

pr_def(*[1,2,3]) # => [1] + [2,3]
It should be: "1 + [2, 3]"

pr_lambda = lambda{|recv,args| puts "#{recv.inspect} + #{args.inspect}"}
pr_lambda.call(
[1,2,3]) # => [1] + [2,3]

Also it should be "1 + [2, 3]".

--
// SASADA Koichi at atdot dot net

Actions

Also available in: Atom PDF

Like0
Like0Like0