From: Nathan Beyer on 11 Mar 2010 18:55 Given a simple class like this class MyClass attr_reader :value def value=(val) @value = val.to_s end end Why the the following code return the value passed and not the value assigned? c = MyClass.new result = c.value = 2 puts result puts c.value This is currently outputing 2 "2" The results are the same when MyClass is modified to be this. class MyClass attr_reader :value def value=(val) @value = val.to_s return @value end end Shouldn't the result of the 'value=' method be the return value?
From: Tony Arcieri on 11 Mar 2010 18:57 [Note: parts of this message were removed to make it a legal post.] Setters always return the value they were originally assigned On Thu, Mar 11, 2010 at 4:55 PM, Nathan Beyer <nbeyer(a)gmail.com> wrote: > Given a simple class like this > > class MyClass > attr_reader :value > def value=(val) > @value = val.to_s > end > end > > Why the the following code return the value passed and not the value > assigned? > > c = MyClass.new > result = c.value = 2 > puts result > puts c.value > > This is currently outputing > > 2 > "2" > > The results are the same when MyClass is modified to be this. > > class MyClass > attr_reader :value > def value=(val) > @value = val.to_s > return @value > end > end > > Shouldn't the result of the 'value=' method be the return value? > > -- Tony Arcieri Medioh! A Kudelski Brand
From: Yukihiro Matsumoto on 11 Mar 2010 19:03 Hi, In message "Re: result of assignment is not the return value" on Fri, 12 Mar 2010 08:55:25 +0900, Nathan Beyer <nbeyer(a)gmail.com> writes: |Shouldn't the result of the 'value=' method be the return value? It's a design choice. We defined the value of the assignment as the value of the right hand expression, not the return value from the assigning method. matz.
From: Nathan Beyer on 11 Mar 2010 22:39 On Thu, Mar 11, 2010 at 6:03 PM, Yukihiro Matsumoto <matz(a)ruby-lang.org> wrote: > Hi, > > In message "Re: result of assignment is not the return value" > Â Â on Fri, 12 Mar 2010 08:55:25 +0900, Nathan Beyer <nbeyer(a)gmail.com> writes: > > |Shouldn't the result of the 'value=' method be the return value? > > It's a design choice. Â We defined the value of the assignment as the > value of the right hand expression, not the return value from the > assigning method. > > Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â matz. Ahh. Thanks. I figured it was something like that, but I couldn't find any concrete reference to it. > >
From: Hal Fulton on 12 Mar 2010 02:30 [Note: parts of this message were removed to make it a legal post.] On Thu, Mar 11, 2010 at 9:39 PM, Nathan Beyer <nbeyer(a)gmail.com> wrote: > On Thu, Mar 11, 2010 at 6:03 PM, Yukihiro Matsumoto <matz(a)ruby-lang.org> > wrote: > > > > It's a design choice. We defined the value of the assignment as the > > value of the right hand expression, not the return value from the > > assigning method. > > > > matz. > > Ahh. Thanks. I figured it was something like that, but I couldn't find > any concrete reference to it. > > I have thought about this for awhile. One rationalization is this. Think of "stacked assignment" which Ruby borrows from C. x = y = 5 An accessor acts much like a simple assignment: x = foo.bar = 5 # same as: x = (foo.bar = 5) In this case, it would be weird if x ended up being anything other than 5. Hal
|
Pages: 1 Prev: Each_char Next: Beware prelink and compiling ruby from source |