Bug #7773

Calling send("attribute=", value) returns nil instead of value

Added by Chris Gaffney about 1 year ago. Updated about 1 year ago.

[ruby-core:51821]
Status:Closed
Priority:Normal
Assignee:Nobuyoshi Nakada
Category:core
Target version:2.0.0
ruby -v:2.0.0-rc1 Backport:

Description

When calling an attribute writer created using either attraccessor or attrwriter via send nil is returned from the send call instead of the new value.

I've attached a test case that is failing on current head (51794) and 2.0.0-rc1. I've done a git bisect and it looks like commit 37228 (db1e99cd) is the first offending commit.

I've been able to reproduce on OS X 10.8 and Ubuntu 12.04.

send_test.rb Magnifier (280 Bytes) Chris Gaffney, 02/03/2013 05:41 PM

Associated revisions

Revision 39121
Added by Marc-Andre Lafortune about 1 year ago

  • vminsnhelper.c: attrwriter should return its argument [Bug #7773]

  • test/ruby/test_basicinstructions.rb: Test for above

History

#1 Updated by Nobuyoshi Nakada about 1 year ago

  • Status changed from Open to Feedback

Why do you consider it a bug?

#2 Updated by Marc-Andre Lafortune about 1 year ago

  • Category set to core
  • Status changed from Feedback to Open
  • Target version set to 2.0.0

nobu (Nobuyoshi Nakada) wrote:

Why do you consider it a bug?

I don't know about Chris, but here is why I consider it a bug:
* returning nil is less useful than value
* it is less intuitive too (to me)
* it is a change of behavior (possibly breaking some existing code)
* this change of behavior was never announced, discussed or approved AFAIK
* the referred commit has no stated or implied intention to change this
* the referred commit has no test to that effect

What I wonder is: why do you consider it could possibly be a feature?

#3 Updated by Chris Gaffney about 1 year ago

I consider it a bug because the behavior only happens using send. If I were to call bar= directly (see below) it returns the value that was set.

class Foo
attr_writer :bar
end

Foo.new.bar = "test" # => "test"

Should send behave differently than sending the message directly?

#4 Updated by Yusuke Endoh about 1 year ago

  • Status changed from Open to Assigned
  • Assignee set to Nobuyoshi Nakada

gaffneyc (Chris Gaffney) wrote:

I consider it a bug because the behavior only happens using send.

Actually, it is not a bug itself.
An assignment-like method call always returns its right value rather than the return value from the method.
The following code shows the difference:

class Foo
def foo=(x)
"foo"
end
end
x = Foo.new
p(x.foo = 42) #=> 42
p(x.send(:foo=, 42)) #=> "foo"

However, as MarcAndre said, a method defined by attr_accessor used to return its argument, at least in ruby 1.9.3p194.
There seems to be a regression. Nobu, could you investigate it?

Yusuke Endoh mame@tsg.ne.jp

#5 Updated by Marc-Andre Lafortune about 1 year ago

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

This issue was solved with changeset r39121.
Chris, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • vminsnhelper.c: attrwriter should return its argument [Bug #7773]

  • test/ruby/test_basicinstructions.rb: Test for above

#6 Updated by Marc-Andre Lafortune about 1 year ago

I meant to assign it to myself... in any case, it's fixed with r39121.

Also available in: Atom PDF