Professional Web Applications Themes

Return value of foo= - Ruby

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

  1. #1

    Default 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
    Gavin Guest

  2. #2

    Default Re: Return value of foo=

    Hi,

    In message "Return value of foo="
    on 04/02/13, Gavin Kistner <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.


    Yukihiro Guest

  3. #3

    Default Re: Return value of foo=

    Yukihiro Matsumoto wrote: 

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

  4. #4

    Default Re: Return value of foo=

    Sam Roberts wrote:
     
    >
    >
    > 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
    >
    >[/ref]

    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.


    Gennady Guest

  5. #5

    Default Re: Return value of foo=

    Hi,

    In message "Re: Return value of foo="
    on 04/02/13, Sam Roberts <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.

    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.


    Yukihiro Guest

  6. #6

    Default Re: Return value of foo=

    Hi,

    In message "Re: Return value of foo="
    on 04/02/13, Gavin Kistner <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. :)

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

    matz.


    Yukihiro Guest

Similar Threads

  1. what would this return
    By realtime158 in forum Macromedia Flex General Discussion
    Replies: 2
    Last Post: July 9th, 05:53 PM
  2. Return me
    By LostMIke in forum Macromedia Dynamic HTML
    Replies: 3
    Last Post: November 15th, 04:42 PM
  3. return xml
    By Poppy in forum ASP.NET Web Services
    Replies: 3
    Last Post: April 27th, 04:01 PM
  4. goToNetPage, return or not return
    By GONS in forum Macromedia Director Basics
    Replies: 1
    Last Post: January 16th, 08:44 PM
  5. anyway to return more than one value?
    By Chris in forum PHP Development
    Replies: 4
    Last Post: November 6th, 01:16 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139