From: Dan Croak Date: 2007-08-10T04:20:06+09:00 Subject: Using the spaceship operator to compare nil? Hey folks, I have a test helper in a Rails app called assert_ordered_by def assert_ordered_by(attribute, collection, direction) collection = collection.to_s return true if assigns["#{collection}"].length < 2 option_one = assigns["#{collection}"][0].send(attribute) option_two = assigns["#{collection}"][1].send(attribute) assert option_one.send(direction, option_two), "#{option_one} is not #{direction} #{option_two}" end It works well in all the cases I've tried thus far except when dealing with nil. I want to teach the method to treat nil the same way SQL treats NULL because the collections will often contain ActiveRecord objects. I have the idea in my head that I need to use the spaceship operator (<=>) to do this, but am unsure about how... Any ideas? Thanks, Dan Croak http://thoughtbot.com/