Return value of foo=

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

puts t3.date
=> 11

puts t3
=> Wed Feb 11 14:37:13 MST 2004

Quoteing gavin@refinery.com, on Fri, Feb 13, 2004 at 04:59:58AM +0900:

The question is, is it possible to change the return value of a foo=
method to NOT be the argument?

Sure, lots of builtin classes do it, for example.

It works as you expect:

  [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>
  irb(main):004:0> f.bar
  9

So, I guess the question is why it don't work for you... and I have no
idea. :slight_smile:

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

puts t3.date
=> 11

puts t3
=> Wed Feb 11 14:37:13 MST 2004

Hi,

···

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.

puts t3.send(:date=, 11)

						matz.

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... :slight_smile: 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:

Yukihiro Matsumoto wrote:

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. :slight_smile:

Sam Roberts wrote:

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… :slight_smile: 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:

Hi,

Ok, now I’m the confused one… :slight_smile: 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.

irb(main):001:0> RUBY_VERSION
=> “1.8.1”
irb(main):002:0> RUBY_RELEASE_DATE
=> “2004-02-03”
irb(main):003:0> class Foo; def bar=(b); @bar = b; self; end; def bar; @bar; end; end
=> nil
irb(main):004:0> f = Foo.new
=> #Foo:0x40347588
irb(main):005:0> f.bar = 9
=> 9

						matz.
···

In message “Re: Return value of foo=” on 04/02/13, Sam Roberts sroberts@uniserve.com writes:

Hi,

···

In message “Re: Return value of foo=” on 04/02/13, Gavin Kistner gavin@refinery.com writes:

Thanks for the quick confirmation. While I understand why this is, take
note that at least 1 user (me) was surprised by this choice. :slight_smile:

YMMV. The point is how many users would have hard time to understand
the rationale. You understood, fine.

						matz.