Ruby on Rails Monday, January 20, 2014

On Mon, Jan 20, 2014 at 1:30 PM, Peter <peter@poproj.com> wrote:
>> class Confuse
>> def confusion=(obj)
>> @obj = obj
>> return 'expected answer'
>> end
>> end
>> c = Confuse.new
>>
>> c.confusion = 'this should be wrong'
>
>
> This returns 'this should be wrong'.

Ruby setters always ignore your explicit return and the last line as a
return and return the attribute given it... as a feature, and this
should be expected behavior as all Ruby setters behave this way.

This means that you define :hello= and it accepts the attribute :world
and you give :world the value of "foo" it will return "foo" because
that was the value it was given for it's assignment and logically that
is the value it should return if it returns anything at all. To
return "expected answer" would be ambiguous no matter how you try to
play it.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAM5XQnwVQFYdctcLf_XLZup4MxFdQV-0gn41c1y5on8tWoo%2BRQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment