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’

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

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

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

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

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

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?

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 H.” [email protected] escreveu:

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

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 # => 10

Here how 11 ?

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

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

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.