Forum: Ruby-core [Bug #1698] Incorrect return value for writer methods.

Posted by Mike Ryan (Guest)
on 2009-06-29 01:49
(Received via mailing list)
Bug #1698: Incorrect return value for writer methods.
http://redmine.ruby-lang.org/issues/show/1698

Author: Mike Ryan
Status: Open, Priority: Normal
ruby -v: ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-darwin9]

I found the following behavior this past week and found it a bit odd.

irb(main):001:0> class Foo
irb(main):002:1>  def bar=(bla)
irb(main):003:2>   @bar = bla * 2
irb(main):004:2>  end
irb(main):005:1>  def bar
irb(main):006:2>   @bar
irb(main):007:2>  end
irb(main):008:1> end
=> nil
irb(main):009:0> a = Foo.new
=> #<Foo:0x13f880>
irb(main):010:0> a.bar = 10
=> 10
irb(main):011:0> a.bar
=> 20
irb(main):012:0> a.send(:bar=, 7)
=> 14
irb(main):013:0> a.bar
=> 14
irb(main):014:0> a.bar=(3)
=> 3
irb(main):015:0> a.bar
=> 6

You'll see that line 10 returns the value that we are passing to the 
bar=(bla) method, but not the actual return value.

Not certain if it was because I wasn't explicitly returning a value, I 
found that this did not matter:

irb(main):001:0> class Foo
irb(main):002:1>  def strange=(bla)
irb(main):003:2>   return "correct return value"
irb(main):004:2>  end
irb(main):005:1> end
=> nil
irb(main):006:0> a = Foo.new
=> #<Foo:0x86df8>
irb(main):007:0> a.strange = "this is strange"
=> "this is strange"
irb(main):008:0> a.send(:strange=, "this is also strange")
=> "correct return value"

I see this in both 1.9.1p129 and 1.8.7p72.
Posted by Mike Ryan (Guest)
on 2009-06-29 01:52
(Received via mailing list)
Issue #1698 has been updated by Mike Ryan.


Forgot to also point out (though I did demonstrate) the fact that if you 
send directly to the method, you get the proper return. Maybe it's part 
of some optimization.
----------------------------------------
http://redmine.ruby-lang.org/issues/show/1698
Posted by Ryan Davis (Guest)
on 2009-06-29 04:17
(Received via mailing list)
On Jun 28, 2009, at 16:48 , Mike Ryan wrote:

> irb(main):002:1>  def bar=(bla)
> irb(main):003:2>   @bar = bla * 2
> irb(main):004:2>  end
> irb(main):005:1>  def bar
> irb(main):006:2>   @bar
> irb(main):007:2>  end
> irb(main):008:1> end

This is intentional. The result of any assignment expression is always
the RHS.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.