Doug Jolley wrote:
> If so, I'm just
> going to have to expand my understanding of what constitutes false in
> Ruby to include the absence of a key from a hash.
>
Your understanding of what 'evaluates' to false in a boolean setting,
like an if statement, is spot on. false obviously evaluates to false,
but the only other thing that evaluates to false is nil.
The thing you need to expand your understanding of is: the possible
return values of the methods you call. A hash returns nil if the key
doesn't exist (or a default value if a default value was set when the
hash was created).
So, yeah you can rely on a hash returning nil if the key doesn't exist,
and also that nil will evaluate to false.
> So, out of
> curiosity, is it your understanding that, in Ruby, false includes the
> absence of a key from a hash?
>
I don't think of it in such abstract terms. If I the key doesn't exist,
the '[]' method returns nil.
> Actually the distinction between nil and nothing
Actually, it's the distinction between nil and false and anything else.
If the object you are evaluating in an if statement is not specifically
nil or false, then it will evaluate to true, period. A concept like
"absence of nothing" is totally irrelevant and not worth thinking about.
Experienced programmers in other languages often get tripped up by ruby
because they expect things like empty strings(""), empty arrays([]) or
empty hashes({}) to evaluate to false. But because none of those things
is specifically false or nil, they evaluate to true. It's that simple.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---