Ruby - Introduction nil


To display a nil value using print or puts, Ruby 1.8 displays "nil," whereas Ruby 1.9 displays an empty string.

To be sure that the string representation of nil is displayed, use p or the inspect method instead of print.

You may also display its class since nil is an instance of NilClass or test whether it is nil using the nil? method:


arr = ['a', 'b', 'c'] 
puts(arr[3].inspect)    #=> nil 
puts(arr[3].class)      #=> NilClass 
p(arr[3])               #=> nil 
puts(arr[3].nil?)       #=> true
# from w  w  w .j  av a 2 s.  c om