Skip to content

Instantly share code, notes, and snippets.

@dwhenry
Created July 27, 2020 13:42
Show Gist options
  • Select an option

  • Save dwhenry/856a10d176ce1e85a52e73624f30c1d7 to your computer and use it in GitHub Desktop.

Select an option

Save dwhenry/856a10d176ce1e85a52e73624f30c1d7 to your computer and use it in GitHub Desktop.
# 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