Csaba Henk
2/20/2005 11:36:00 PM
On 2005-02-20, Navindra Umanee <navindra@cs.mcgill.ca> wrote:
> Often think it would be nice if "" and 0 were treated like nil. Such
> functions could then return "". Heck, NilClass.to_s and NilClass.to_i
> already return "" and 0 respectively.
Now, only false and nil drop you to the "else" branch of a conditional.
Certainly, there are cases when it' not the best behaviour, and you
have to write extra code to get the proper control flow when you test
against 0 and/or "".
If it were the other way around, and "", 0 (and [], {}) would lead to
the "else" branch as well, again, there would occur situations when it's
not the best behaviour and you'd need to write extra code to get things
right.
Now the question is: which of the two behaviours is the right thing more
often?
My experience is that the present way is the more optimal.
It's more cleaner as well. There is no intrinsic difference between ""
and "a", 0 and 1, [] and [1]. It would be artificial to force an
instance-sensitive policy when testing agains Strings, Integers and
Arrays.
Those guys who make a difference when using them in a conditional are
clearly separated into their respective cages (I mean, classes). That's
nice...
Csaba