Created
April 13, 2009 22:42
-
-
Save jqr/94799 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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