Confusion with the setter method return value

I was writing some code, and there I found something odd, which made me
surprised :

class Object
  def bar
    #"I got #{@bar}"
  end
  def bar=(bar)
    'return'
  end
end

self.bar = 10 # => 10 <~~ why not the value 'return', instead 10
send(:bar=,10) # => 'return'

···

--
Posted via http://www.ruby-forum.com/.

Well it is by design, setter return right hand value rather then normal
return value.
See this
https://groups.google.com/d/msg/comp.lang.ruby/cSnpqgspnhw/5be3F3hQSPYJ
-Arun

···

On Wed, Jan 8, 2014 at 7:22 PM, Arup Rakshit <lists@ruby-forum.com> wrote:

I was writing some code, and there I found something odd, which made me
surprised :

class Object
  def bar
    #"I got #{@bar}"
  end
  def bar=(bar)
    'return'
  end
end

self.bar = 10 # => 10 <~~ why not the value 'return', instead 10
send(:bar=,10) # => 'return'

--
Posted via http://www.ruby-forum.com/\.

This also confuses me.

setter return right hand value rather then normal return value.

Isn't that truly a logical thing? After all, it is a method call. I can
use setters without the =

  def set_foo(i)
    @foo = i
  end

···

--
Posted via http://www.ruby-forum.com/\.

Simply because the parser treats "=" as a special case. Using "send" is
different.

···

--
Posted via http://www.ruby-forum.com/.

Arun kant sharma wrote in post #1132527:

Well it is by design, setter return right hand value rather then normal
return value.
See this
https://groups.google.com/d/msg/comp.lang.ruby/cSnpqgspnhw/5be3F3hQSPYJ
-Arun

Thanks Arun. I am also from India. Do you know any company who is hiring
ruby developers? If you do have, would you refer me ? This is my account
- User Arup Rakshit - Stack Overflow .

···

--
Posted via http://www.ruby-forum.com/\.

In Ruby they are treated differently, there's a syntax sugar for methods
ending with the "=" character.

So,

self.setter=(value)

And

self.setter = value

Are the same.

There's other internal differences. One of them I'm aware of is, on setters
the returned values is always the value of the assignment (right value) not
the value returned by the method.

Abinoam Jr.

···

Em 13/01/2014 05:25, "Marc Heiler" <lists@ruby-forum.com> escreveu:

This also confuses me.

> setter return right hand value rather then normal return value.

Isn't that truly a logical thing? After all, it is a method call. I can
use setters without the =

  def set_foo(i)
    @foo = i
  end

--
Posted via http://www.ruby-forum.com/\.

Yes, (Joel) when using send it bypass this trick.
Using send even bypass private(ness) of the method.

class Foo
  private
  def my_private
    "You got me"
  end
end

foo = Foo.new

foo.my_private
# => NoMethodError: private method `my_private' called for
#<Foo:0x007f90fa191530>

foo.send(:my_private)
# => "You got me"

Abinoam Jr.

···

On Mon, Jan 13, 2014 at 9:20 AM, Joel Pearson <lists@ruby-forum.com> wrote:

Simply because the parser treats "=" as a special case. Using "send" is
different.

--
Posted via http://www.ruby-forum.com/\.

Marc Heiler wrote in post #1132943:

This also confuses me.

setter return right hand value rather then normal return value.

Isn't that truly a logical thing? After all, it is a method call. I can
use setters without the =

  def set_foo(i)
    @foo = i
  end

Yes, of-course. But in that case `set_foo` will not be called as
**setter**. It is simply a method which is setting/assigning the value
to the instance variable @foo.

···

--
Posted via http://www.ruby-forum.com/\.

I don't know now but I will keep you in mind.
-Arun

···

On Thu, Jan 9, 2014 at 12:22 AM, Arup Rakshit <lists@ruby-forum.com> wrote:

Arun kant sharma wrote in post #1132527:
> Well it is by design, setter return right hand value rather then normal
> return value.
> See this
> https://groups.google.com/d/msg/comp.lang.ruby/cSnpqgspnhw/5be3F3hQSPYJ
> -Arun

Thanks Arun. I am also from India. Do you know any company who is hiring
ruby developers? If you do have, would you refer me ? This is my account
- http://stackoverflow.com/users/2767755/arup-rakshit .

--
Posted via http://www.ruby-forum.com/\.

Abinoam Jr. wrote in post #1132967:

In Ruby they are treated differently, there's a syntax sugar for methods
ending with the "=" character.

There's other internal differences. One of them I'm aware of is, on
setters
the returned values is always the value of the assignment (right value)
not
the value returned by the method.

class Foo
  def bar=(bar)
    11
  end
end

foo = Foo.new

# 10 it is ok..
foo.bar = 10 # =>

# Here how 11 ?
foo.send(:bar=,10) # => 11

···

--
Posted via http://www.ruby-forum.com/\.

Arun kant sharma wrote in post #1132582:

I don't know now but I will keep you in mind.
-Arun

Thanks Arun. In which company do you work?

···

--
Posted via http://www.ruby-forum.com/\.

I am a student here at IITK. You can contact me at @arunkants on twitter.
Batter then spaming people's inboxes . :slight_smile:

···

On Fri, Jan 10, 2014 at 11:10 PM, Arup Rakshit <lists@ruby-forum.com> wrote:

Arun kant sharma wrote in post #1132582:
> I don't know now but I will keep you in mind.
> -Arun

Thanks Arun. In which company do you work?

--
Posted via http://www.ruby-forum.com/\.