# Redefined accessor and return value

**URL:** <https://www.ruby-forum.com/t/redefined-accessor-and-return-value/172345>\
**Category:** Rails\
**Created:** [July 19, 2009, 11:35pm UTC](https://www.ruby-forum.com/t/redefined-accessor-and-return-value/172345 "2009-07-19T23:35:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [July 19, 2009, 11:35pm UTC](https://www.ruby-forum.com/t/redefined-accessor-and-return-value/172345/1 "2009-07-19T23:35:19Z")

</div>

Hi,

I have a class that has an attribute named “an\_attribute”. I would like  
to  
define the following function

def an\_attribute=(new\_value)  
return false  
end

The problem is every time I call the function, the return value is  
always  
“new\_value” instead of “false.” Is there anyway for me to make it  
return  
“false”? Of course, the above simplistic example is only used to  
illustrate  
what I want to ask.

Thanks.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [July 20, 2009, 12:01am UTC](https://www.ruby-forum.com/t/redefined-accessor-and-return-value/172345/2 "2009-07-20T00:01:02Z")

</div>

Hi –

On Sun, 19 Jul 2009, Ease B. wrote:

> "false."Â Is there anyway for me to make it return “false”?Â Of course, the above simplistic example  
> is only used to illustrate what I want to ask.Â

I don’t think there’s any way. The = methods are engineered so as to  
provide assignment-like semantics, and assignments always return their  
right-hand side.

You can circumvent this if you use send:

> > def x=(\*)  
> > “Explicit return value”  
> > end  
> > =\> nil  
> > self.x = 20  
> > =\> 20  
> > send(:x=, 20)  
> > =\> “Explicit return value”

(Thanks to Rick DeNatale for reminding me of the send thing in a  
recent ruby-talk thread about this 🙂

But using send with a = method kind of defeats the purpose.

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Now available: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))  
Training! Intro to Ruby, with Black & Kastner, September 14-17  
(More info: [http://rubyurl.com/vmzN](http://rubyurl.com/vmzN))

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [July 20, 2009, 2:37am UTC](https://www.ruby-forum.com/t/redefined-accessor-and-return-value/172345/3 "2009-07-20T02:37:37Z")

</div>

David:

Thank you! You saved me so much time. I have decided to name the  
setter “set\_attribute\_A” instead of redefining “attribute\_A=” in order  
to get a customized return value. Now I am trying to figure out how  
to make the default setter private so that outside of the instance,  
the setter cannot be called.

I’ve posted another thread regarding that issue. See here:  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/727e1d3fb3e67987](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/727e1d3fb3e67987)

Thanks,

LBD.
