When nothing isn't

Jeff Pritchard wrote:

One of the strangest things for me, coming from the "C" world to Ruby
was how much attention "nil" gets. Sure, NULL is there in C, but it's
about as interesting as its value.

In Ruby, it seems as if nil is an extra value, stuffed in somewhere
around zero, but not really there or anywhere else. It seems to be
almost ubiquitous in Ruby. Quite strange for something that is so close
to non-existent.

When writing rails, I find myself experimenting with various and sundry
ways of checking for nil and not really satisfied with any of them. I
would like to find out from the group here what are the most standard
ways to check for nil in a couple of common situations.

One of the most common for me seems to be that of a session key that I
want to do something with but I have to make sure it is not nil first.
Let's say we have a session key that is expected to have an array in it,
and we want to check the size of the array, I wind up using a trinary:

the_size = (session[:somethingorotherarray].nil?) ? 0 :

Is there a more graceful way of doing that? I keep ending up with a
bunch of those and also if statements with
(!session[:somethingorotherarray].nil?). Looks really ugly to me.

Do others use "defined?" more often, and can you use that on a session
key? Just seems like I'm working harder at avoiding nothing than I
should. How are the rest of you dealing with nothing of this variety
and other types of nothing that happen along frequently in a Rails app?

A hash will return nil if you try to access it with a non-existent key:

irb(main):001:0> hash = Hash.new
=> {}
irb(main):002:0> hash[:blah]
=> nil

This allows you get to rid of those .nil? methods on hashes if you don't like them.

There is also "||=". As in:

irb(main):003:0> hash[:size] ||= 0
=> 0
irb(main):004:0> hash[:size] ||= 10
=> 0

which is the equivalent of

hash[:size] = hash[:size] || 0

so you could do something like:

irb(main):018:0> hash = {}
=> {}
irb(main):019:0> size = (hash[:size] ||= 10)
=> 10
irb(main):020:0> size
=> 10
irb(main):021:0> hash[:size]
=> 10