Created
July 11, 2011 13:09
-
-
Save andreacfm/1075795 to your computer and use it in GitHub Desktop.
ruby_comparable
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
| class Person | |
| include Comparable | |
| attr_reader :name | |
| def initialize(name) | |
| @name = name | |
| end | |
| def <=> other | |
| self.name <=> other.name | |
| end | |
| end | |
| p1 = Person.new 'Andrea' | |
| p2 = Person.new 'Fabio' | |
| p3 = Person.new 'Luigi' | |
| p p1 < p2 #true | |
| p p2 < p1 #false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment