From: Dan Croak Date: 2007-08-10T06:29:59+09:00 Subject: Re: Using the spaceship operator to compare nil? I forgot to mention that this is called, for example, with: assert_ordered_by :last_name, :users, "<=" I'm also moving away from using the spaceship operator into conditional logic land because I was having difficulty dealing (mentally) with comparing nil to various types (dates, strings, integers, etc.). Here's the latest non-working code... :/ def assert_ordered_by(attribute, collection, direction) collection = assigns["#{collection.to_s}"] return true if collection.length < 2 collection.sort do |a, b| if a.nil? && !b.nil? return false if direction.include?(">") # nil cannot be greater than anything except another nil elsif b.nil? && !a.nil? return false if direction.include?("<") # nil cannot be greater than anything except another nil elsif !a.nil? && !b.nil? assert a.send(attribute).send(direction, b.send(attribute)) elsif a.nil? && b.nil? return true end end end