Why does:
something = false
always return false?
Since it is an assignment and not a comparison, it seems it would make more sense to return the result (success/failure) of the assignment (like pretty much every other assignment you do).
EDIT - this is wrong, as the answers below point out. My bad. Slight brain fart. See the original impetus for this question in the comments below.
What happens if some_boolean_var = false failed for whatever reason (unlikely, but theoretically possible, especially if you defined a custom method for this action)? That would also return false, so you have no indication as to whether the assignment actually worked.
It basically just means you can't combine assignment and comparison in one line like you can do with nearly everything else in Rails. For example, you can't do:
if something = false (note the single =)
do stuff
end
I mean you can do it but not if you wanted to check and make sure the assignment succeeded first.
Just really weird to me.
I'm sure there is a reason for this so please enlighten me :)
Thanks in advance!
nilandfalseare false. Everything else is true. – Oct 12 '13 at 19:29if a = b:does not compile. Assignment is a statement, and chained assignment is encoded into the grammar for assignment statements, unlike C, where it is an intentional side effect of the fact that assignment is an expression and that its return value is the assigned value. For all I know, this was a deliberate design decision, to prevent theif (foo = 1)kind of bug. – tdammers Oct 12 '13 at 21:58