Caffeine-Powered Life

Running Through the Ruby Koans

If you’re new to Ruby, check out the koans. They’re a great way to learn some of the finer details of the Ruby language.

Check out this little gem I picked up from the second koan on learning about nil.

  • In Ruby, everything is an object.
  • In Ruby, even nil is an object. It is not a pointer exception that you get in other languages.
  • The nil object has methods on it. You can use nil.to_s, nil.inspect, and nil.nil?, because all Ruby objects have those methods defined and inherited from the base object class.

Finally, check out this little artifact from the comments.


# THINK ABOUT IT:

#

# Is it better to use

#    obj.nil?

# or

#    obj == nil

# Why?

Of course, after doing the koans you’ll realize that the Rubyist will use the first version as a form of inspection, where other developers from other languages would do the second version.

Comments