Hi --

On Sun, 19 Jul 2009, Ease Bus wrote:

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

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
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to