Project

General

Profile

Actions

Bug #1393

closed

Multiple assignment does not call to_a but to_ary

Added by scritch (Vincent Isambart) over 15 years ago. Updated over 13 years ago.

Status:
Rejected
Target version:
ruby -v:
1.9.1p0, 1.9.2dev (2009-04-19 trunk 23210)
Backport:

Description

=begin
In Ruby 1.9, when Ruby is expecting an array and does not get one (for example in splat arguments), it calls the to_a method on the object.
For example, if x is not an array, "y = *x" tries to call x.to_a.
However, in multiple assignments, it does not try to call to_a but to_ary.
For example, "*y = x" tries to call x.to_ary.
Should not Ruby try to call to_a in both cases? It looks like a bug to me.
Is there a particular reason this was done?
=end

Actions #1

Updated by yugui (Yuki Sonoda) over 15 years ago

  • Assignee set to ko1 (Koichi Sasada)

=begin

=end

Actions #2

Updated by yugui (Yuki Sonoda) over 15 years ago

  • Status changed from Open to Assigned
  • Target version set to 1.9.2

=begin

=end

Actions #3

Updated by mame (Yusuke Endoh) over 14 years ago

  • Category set to core
  • Assignee changed from ko1 (Koichi Sasada) to matz (Yukihiro Matsumoto)

=begin
Hi,

2009/4/21 Vincent Isambart :

In Ruby 1.9, when Ruby is expecting an array and does not get one (for example in splat arguments), it calls the to_a method on the object.
For example, if x is not an array, "y = *x" tries to call x.to_a.
However, in multiple assignments, it does not try to call to_a but to_ary.
For example, "*y = x" tries to call x.to_ary.

This issue is still reproducable:

obj = Object.new
def obj.to_a; [1, 2]; end
def obj.to_ary; [3, 4]; end
def foo(x, y); p [x, y]; end

a, b = *obj; p [a, b] #=> [1, 2] on 1.9, [3, 4] on 1.8
foo(*obj) #=> [1, 2] on 1.9, [3, 4] on 1.8

Should not Ruby try to call to_a in both cases? It looks like a bug to me.

Agreed. I think that to_ary should be called because the both cases do
implicit conversion.

Is there a particular reason this was done?

I guess that it is just a slip-up when removing to_splat (r14063).

Matz, is my guess correct?
Yugui, do you allow the spec change (fix) in 1.9.2?

FYI: if this is fixed, 21 failures/errors occur on RubySpec.
They depend on the current behavior implicitly and explicitly, such as:

[*nil].should == []

a = loop do break *nil; end; a.should == []

obj = Object.new
def obj.to_a
[1, 2]
end
return *obj

--
Yusuke ENDOH
=end

Actions #4

Updated by mame (Yusuke Endoh) over 14 years ago

  • Priority changed from Normal to 5

=begin
Hi, Matz

Could you please answer this ticket?

For example, if x is not an array, "y = *x" tries to call x.to_a.
However, in multiple assignments, it does not try to call to_a but to_ary.
For example, "*y = x" tries to call x.to_ary.

This is philosophical, but really significant bug against
convention of Ruby's type conversion, I think. I can't
ignore this without your response.

--
Yusuke Endoh
=end

Actions #5

Updated by matz (Yukihiro Matsumoto) over 14 years ago

=begin
Hi,

In message "Re: [ruby-core:29509] [Bug #1393] Multiple assignment does not call to_a but to_ary"
on Wed, 14 Apr 2010 21:11:35 +0900, Yusuke Endoh writes:

|Could you please answer this ticket?
|
|> For example, if x is not an array, "y = *x" tries to call x.to_a.
|> However, in multiple assignments, it does not try to call to_a but to_ary.
|> For example, "*y = x" tries to call x.to_ary.
|
|This is philosophical, but really significant bug against
|convention of Ruby's type conversion, I think. I can't
|ignore this without your response.

OK, to_a means an explicit array conversion, so it should not be used
for implicit conversion a in "*y = x". On the other hand, I consider
"x" as a form of explicit conversion, i.e. shorthand for "(x.to_a)",
so that the current 1.9 behavior is intentional.

						matz.

=end

Actions #6

Updated by matz (Yukihiro Matsumoto) over 14 years ago

  • Status changed from Assigned to Rejected

=begin

=end

Actions #7

Updated by mame (Yusuke Endoh) over 14 years ago

=begin
Hi,

I see. Thank you for your clarifying!

--
Yusuke Endoh
=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0