Skip to content

Instantly share code, notes, and snippets.

@jqr
Created April 13, 2009 22:42
Show Gist options
  • Save jqr/94799 to your computer and use it in GitHub Desktop.
Save jqr/94799 to your computer and use it in GitHub Desktop.
# An array of String subclasses does not sort using <=> on the subclass, WTF?
class StringSubclass < String
def <=>(other)
puts "using <=> in #{self.class}: #{caller.first}"
super
end
end
StringSubclass.new <=> StringSubclass.new
# => using <=> in StringSubclass: untitled:10
[StringSubclass.new, StringSubclass.new].sort
# => NOTHING!
# Let's try another built-in class.
class ArraySubclass < Array
def <=>(other)
puts "using <=> in #{self.class}: #{caller.first}"
super
end
end
ArraySubclass.new <=> ArraySubclass.new
# => using <=> in ArraySubclass: untitled:25
[ArraySubclass.new, ArraySubclass.new].sort
# => using <=> in ArraySubclass: untitled:28:in `sort'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment