G
Gerardo Santana Gómez Garrido
2007/10/15 said:I suspect the confusion comes from the fact that your initial post was
titled "nil.to_i returning zero". Apparently the subject was a red
herring. We might have gotten more swiftly to discussing the merits of
your beliefs/suggestions had the subject been titled (for example)
"nil.to_i (and nil.to_s) should not exist".
Let me just precise that I'm ok with nil.to_s.
It's nil.to_i I'm uncomfortable with
This isn't criticism - I know well how it sometimes occurs that only
after a discussion has taken place do I realize what question or
statement I was really trying to make. The discussion is part of the
clarifying process.
So, having refocused the discussion on the question: "Should NilClass
have a #to_i method?", let me throw in my opinion:
Various objects have methods that return an instance of a different
class. The most obvious is the #to_s method. By extension, your
argument that nil.to_i should not return 0 because 0 is a truth value
would further imply that false.to_s should not return "false" because
all strings are also truth values.
Not really, if we understand #to_s as a form of serialization.
nil.to_s doesn't return "nil", nor "0", but "".
Is it acceptable for an object (of
any class) to have a method that returns an object that behaves
differently under core language concepts (like boolean evaluation)? I
would say "yes".
And so do I.
So, what other reasons exist for wanting to do away with nil.to_i? The
main (only other?) argument seems to be, "nil is special, and I want
to know for sure when I get a nil value. It's so special, I should get
a NoMethodError if I try to do (just about) anything to it."
I agree that it's special. I've certainly seen that foo.id =3D> 4 (when
foo is unexpectedly nil) can cause confusion.
However, which of the following is 'better'?
# Assume nil.to_i is not available
def foo( bar, jim )
bar =3D 0 if bar.nil? # explicit setting
a =3D bar.to_i * 2
b =3D jim.to_i * 2 # I want it to blow up if jim is nil
end
I would write that as:
# Assume nil.to_i is not available
def foo( jim, bar =3D 0)
a =3D bar.to_i * 2
b =3D jim.to_i * 2 # I want it to blow up if jim is nil
end
# Assume nil.to_i is available
def foo( bar, jim )
a =3D bar.to_i * 2 # Magically treat nil as zero
raise "Jim can't be nil!" if jim.nil?
b =3D jim.to_i * 2
end
If the fitness criterion for 'better' in this case is 'less lines of
code', then it depends on which case is more prevalent.
If it were that case, my version is shorter.
If the fitness
criterion is instead 'safer', then not having to_i seems to make sense
to me. If the fitness criterion is 'more convenient' or 'more
expected', then having to_i makes sense to those for whom 0 is a
reasonable integer representation of nil.
If I were to advocate removing nil.to_i, I would probably advocate
removing nil.to_s for the same reasons...and then I would really,
I wouldn't. See reason above.
really hate life in the Rails world (and in other string interpolation
areas). For that reason alone I can't personally vote for getting rid
of nil.to_i.
--=20
Gerardo Santana