Project

General

Profile

Bug #9448

Assignment with overridden []= returns return value of method rather than new value

Added by Simon Eskildsen over 2 years ago. Updated over 2 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
[ruby-core:60071]

Description

When calling []= the assignment will return the return value of the []= method, rather than the value of the right-hand side, which is the behaviour in 2.1.0-p0, 2.0.0-p353 and 1.9.3-p484.

Example code:

class Animal                                                                                                                  
  def []=(key, new_value)                                                                                                      
    "walrus"                                                                                                                 
  end                                                                                                                          
end                                                                                                                            
animal = Animal.new                                                                                                               
puts animal['current'] = "unicorn" 

In trunk, this returns 'walrus'. In other releases (tested in the ones mentioned above), this returns 'unicorn'.

Associated revisions

Revision 44705
Added by Nobuyoshi Nakada over 2 years ago

compile.c: assignment result of aset_with

  • compile.c (iseq_compile_each): result of assignment should be its rhs instead of returned value from a method. [Bug #9448]

Revision 44705
Added by Nobuyoshi Nakada over 2 years ago

compile.c: assignment result of aset_with

  • compile.c (iseq_compile_each): result of assignment should be its rhs instead of returned value from a method. [Bug #9448]

Revision 44705
Added by Nobuyoshi Nakada over 2 years ago

compile.c: assignment result of aset_with

  • compile.c (iseq_compile_each): result of assignment should be its rhs instead of returned value from a method. [Bug #9448]

History

#1 [ruby-core:60082] Updated by Nobuyoshi Nakada over 2 years ago

  • Status changed from Open to Rejected

It's a spec.
An assignment returns the assigned value, not the value returned by the method.

#2 [ruby-core:60083] Updated by Eric Wong over 2 years ago

nobu@ruby-lang.org wrote:

Status changed from Open to Rejected

It's a spec.
An assignment returns the assigned value, not the value returned by the method.

I think you misread, he is saying assignment returns the value returned
by the method in trunk. It looks like trunk is violating spec.
I think it is r44551, opt_aset_str causing the bug. Fix coming.

#3 [ruby-core:60085] Updated by Nobuyoshi Nakada over 2 years ago

  • Status changed from Rejected to Open

Indeed, thank you.

#4 [ruby-core:60086] Updated by Eric Wong over 2 years ago

normalperson@yhbt.net wrote:

I think you misread, he is saying assignment returns the value returned
by the method in trunk. It looks like trunk is violating spec.
I think it is r44551, opt_aset_str causing the bug. Fix coming.

I fail at fixing this so far. Maybe I need food or sleep, first :x

Work-in-progress here:
http://bogomips.org/ruby.git/patch/?id=1095d48fe6
(also git://bogomips.org/ruby.git bug9448-wip)

#5 [ruby-core:60088] Updated by Nobuyoshi Nakada over 2 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

Applied in changeset r44705.


compile.c: assignment result of aset_with

  • compile.c (iseq_compile_each): result of assignment should be its rhs instead of returned value from a method. [Bug #9448]

#6 [ruby-core:60090] Updated by Nobuyoshi Nakada over 2 years ago

  • Backport changed from 1.9.3: UNKNOWN, 2.0.0: UNKNOWN, 2.1: UNKNOWN to 1.9.3: DONTNEED, 2.0.0: DONTNEED, 2.1: DONTNEED

#7 [ruby-core:60097] Updated by Eric Wong over 2 years ago

nobu@ruby-lang.org wrote:

Applied in changeset r44705.

Thank you! Sorry for the breakage.
I need to study/understand compile.c better :x

#8 [ruby-core:60100] Updated by Simon Eskildsen over 2 years ago

Thanks a lot for the quick fix!

Also available in: Atom PDF