The question is, is it possible to change the return value of a foo=
method to NOT be the argument?
If the answer is no, well, I can sort of see why (consistency of
expectations for something like: bar = obj.foo+=3) but I also feel
like…if it’s a method, it’s a method, whatever syntax is invoking it.
Shouldn’t I be able to control the return value?
Please see the code below.
···
########################
class MutableTime
…
def date @t.day
end
def date=(n)
@t+=(n-date)*DINS
self # < == LOOK HERE!
end
…
end
########################
puts t3 = MutableTime.new(“2/8/2004 2:37:13pm”)
=> Sun Feb 08 14:37:13 MST 2004
puts t3.date
=>8
puts t3.date=11
=> 11 # <== GRR! I want the object instance to be the return value
So, I guess the question is why it don't work for you... and I have no
idea.
Sam
···
If the answer is no, well, I can sort of see why (consistency of
expectations for something like: bar = obj.foo+=3) but I also feel
like...if it's a method, it's a method, whatever syntax is invoking it.
Shouldn't I be able to control the return value?
Please see the code below.
########################
class MutableTime
...
def date @t.day
end
def date=(n)
@t+=(n-date)*DINS
self # < == LOOK HERE!
end
...
end
########################
puts t3 = MutableTime.new("2/8/2004 2:37:13pm")
=> Sun Feb 08 14:37:13 MST 2004
puts t3.date
=>8
puts t3.date=11
=> 11 # <== GRR! I want the object instance to be the return value
In message “Return value of foo=” on 04/02/13, Gavin Kistner gavin@refinery.com writes:
The question is, is it possible to change the return value of a foo=
method to NOT be the argument?
The value of assignment is defined to be its right hand value, even
when it calls a method internally. You need to call “date=” method
explicitly to receive return value, e.g.
Quoteing matz@ruby-lang.org, on Fri, Feb 13, 2004 at 05:44:48AM +0900:
>The question is, is it possible to change the return value of a foo=
>method to NOT be the argument?
The value of assignment is defined to be its right hand value, even
when it calls a method internally. You need to call "date=" method
explicitly to receive return value, e.g.
puts t3.send(:date=, 11)
Ok, now I'm the confused one... Why does it not look like that in irb?
[ensemble] ~/p/ruby $ irb
irb(main):001:0> class Foo; def bar=(b); @bar = b; self; end; def
bar; @bar; end; end
nil
irb(main):002:0> f = Foo.new
#<Foo:0x16ded4>
irb(main):003:0> f.bar = 9
#<Foo:0x16ded4 @bar=9>
^---- it looks like the value of the expression is "f", not "9".
Is this just an oddity of irb?
irb(main):004:0> f.bar
9
Thanks,
Sam
···
In message "Return value of foo=" > on 04/02/13, Gavin Kistner <gavin@refinery.com> writes:
The value of assignment is defined to be its right hand value, even
when it calls a method internally. You need to call “date=” method
explicitly to receive return value, e.g.
puts t3.send(:date=, 11)
Thanks for the quick confirmation. While I understand why this is, take
note that at least 1 user (me) was surprised by this choice.
The question is, is it possible to change the return value of a foo=
method to NOT be the argument?
The value of assignment is defined to be its right hand value, even
when it calls a method internally. You need to call “date=” method
explicitly to receive return value, e.g.
puts t3.send(:date=, 11)
Ok, now I’m the confused one… Why does it not look like that in irb?
[ensemble] ~/p/ruby $ irb
irb(main):001:0> class Foo; def bar=(b); @bar = b; self; end; def
bar; @bar; end; end
nil
irb(main):002:0> f = Foo.new
#Foo:0x16ded4
irb(main):003:0> f.bar = 9
#<Foo:0x16ded4 @bar=9>
^---- it looks like the value of the expression is “f”, not “9”.
Is this just an oddity of irb?
irb(main):004:0> f.bar
9
Thanks,
Sam
In Ruby 1.8.0 it behaves the way Matz explained. However, in Ruby 1.6.8
it still works as in your example.
Gennady.
···
In message “Return value of foo=” > on 04/02/13, Gavin Kistner gavin@refinery.com writes:
Ok, now I’m the confused one… Why does it not look like that in irb?
[ensemble] ~/p/ruby $ irb
irb(main):001:0> class Foo; def bar=(b); @bar = b; self; end; def
bar; @bar; end; end
nil
irb(main):002:0> f = Foo.new
#Foo:0x16ded4
irb(main):003:0> f.bar = 9
#<Foo:0x16ded4 @bar=9>
^---- it looks like the value of the expression is “f”, not “9”.
Try using newer version. I can’t explain everything for every
version.