Attr_writer

Hi!
I'm playjng with this for more than 2 hours - what is wrong here?

class A
  def A.b=(val) # () necessary?
    val+1
  end
end
A.b=3 # returns 3, not 4!

Thanks
Berg

Ruby assignment always returns the right-hand side value. Any side-effects
inside the :b= method would still apply though.

···

On 29/02/2016 8:36 AM, "A Berger" <aberger7890@gmail.com> wrote:

Hi!
I'm playjng with this for more than 2 hours - what is wrong here?

class A
  def A.b=(val) # () necessary?
    val+1
  end
end
A.b=3 # returns 3, not 4!

Thanks
Berg

But this is not an assignment, right?

In this case, the '=' is just part of the name of the 'b=' method.

I think this is something where Ruby gets confused. The method gets invoked
but the return value is wrong.
You can get the expected value by writing A.send(:b=, 1).

But I'm still curious on what's happening there.

I've tried to debug it using binding.pry but couldn't see anything relevant.

Carlos Alonso | Software Engineer | @calonso <https://twitter.com/calonso&gt;

···

On 29 February 2016 at 00:08, Matthew Kerwin <matthew@kerwin.net.au> wrote:

On 29/02/2016 8:36 AM, "A Berger" <aberger7890@gmail.com> wrote:
>
> Hi!
> I'm playjng with this for more than 2 hours - what is wrong here?
>
> class A
> def A.b=(val) # () necessary?
> val+1
> end
> end
> A.b=3 # returns 3, not 4!
>
> Thanks
> Berg
>

Ruby assignment always returns the right-hand side value. Any side-effects
inside the :b= method would still apply though.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hi
Thanks for these [0,1] answers
( and the bookhint [2] ) !

Berg

Hi all,

I am in the middle of reading "The Well-Grounded Rubyist" and correct me if
I am wrong, but I recalled readying that Ruby will treat any method with an
equal sign suffix as an accessor and that would explain why it returns 3
instead of 4.

···

On Monday, 29 February 2016, Carlos Alonso <info@mrcalonso.com> wrote:

But this is not an assignment, right?

In this case, the '=' is just part of the name of the 'b=' method.

I think this is something where Ruby gets confused. The method gets
invoked but the return value is wrong.
You can get the expected value by writing A.send(:b=, 1).

But I'm still curious on what's happening there.

I've tried to debug it using binding.pry but couldn't see anything
relevant.

Carlos Alonso | Software Engineer | @calonso <https://twitter.com/calonso&gt;

On 29 February 2016 at 00:08, Matthew Kerwin <matthew@kerwin.net.au > <javascript:_e(%7B%7D,'cvml','matthew@kerwin.net.au');>> wrote:

On 29/02/2016 8:36 AM, "A Berger" <aberger7890@gmail.com >> <javascript:_e(%7B%7D,'cvml','aberger7890@gmail.com');>> wrote:
>
> Hi!
> I'm playjng with this for more than 2 hours - what is wrong here?
>
> class A
> def A.b=(val) # () necessary?
> val+1
> end
> end
> A.b=3 # returns 3, not 4!
>
> Thanks
> Berg
>

Ruby assignment always returns the right-hand side value. Any
side-effects inside the :b= method would still apply though.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
<javascript:_e(%7B%7D,'cvml','ruby-talk-request@ruby-lang.org');>
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

But this is not an assignment, right?

It is a regular Ruby method call.

In this case, the '=' is just part of the name of the 'b=' method.

Correct.

I think this is something where Ruby gets confused. The method gets invoked
but the return value is wrong.

No, there is no confusion and of course this is intentional. The
reason is simple: Matz wanted to make sure that the result of
"a.b=xyz" is the same as of "c=xyz" to allow for using assignment
methods the same way as assignments to variables.

You can get the expected value by writing A.send(:b=, 1).

Yes, but that is really not what you would want to do when defining
such a method, right?

But I'm still curious on what's happening there.

Matthew described it.

Kind regards

robert

···

On Mon, Feb 29, 2016 at 11:13 AM, Carlos Alonso <info@mrcalonso.com> wrote:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/