Project

General

Profile

Actions

Bug #7773

closed

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

Added by gaffneyc (Chris Gaffney) about 11 years ago. Updated about 11 years ago.

Status:
Closed
Target version:
ruby -v:
2.0.0-rc1
Backport:
[ruby-core:51821]

Description

When calling an attribute writer created using either attr_accessor or attr_writer 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.


Files

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

Updated by nobu (Nobuyoshi Nakada) about 11 years ago

  • Status changed from Open to Feedback

Why do you consider it a bug?

Updated by marcandre (Marc-Andre Lafortune) about 11 years 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?

Updated by gaffneyc (Chris Gaffney) about 11 years 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?

Updated by mame (Yusuke Endoh) about 11 years ago

  • Status changed from Open to Assigned
  • Assignee set to nobu (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

Actions #5

Updated by marcandre (Marc-Andre Lafortune) about 11 years 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.


  • vm_insnhelper.c: attr_writer should return its argument [Bug #7773]

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

Updated by marcandre (Marc-Andre Lafortune) about 11 years ago

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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0