Created
July 27, 2020 13:42
-
-
Save dwhenry/856a10d176ce1e85a52e73624f30c1d7 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
| # sort by example | |
| class Counter | |
| def initialize | |
| @count = 0 | |
| end | |
| def inc | |
| @count += 1 | |
| end | |
| def count | |
| @count | |
| end | |
| end | |
| counter = Counter.new | |
| class Apple | |
| attr_reader :id | |
| def initialize(id, counter) | |
| @id = id | |
| @counter = counter | |
| end | |
| def value | |
| @counter.inc | |
| [id % 3, id] | |
| end | |
| def <=>(other) | |
| value <=> other.value | |
| end | |
| end | |
| apples = 100.times.map { |id| Apple.new(id, counter) }; nil | |
| apples.sort | |
| puts counter.count | |
| # => 1224 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment