Skip to content

Instantly share code, notes, and snippets.

@jtmcmc
Last active December 17, 2015 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtmcmc/2e22235642c8d891137c to your computer and use it in GitHub Desktop.
Save jtmcmc/2e22235642c8d891137c to your computer and use it in GitHub Desktop.
top_five = SortedSet.new
items.each do |key, value|
latest_time = DateTime.parse(key)
top_five << latest_time
if top_five.to_a.length > 5
top_five.to_a.shift
end
end
@jtmcmc
Copy link
Author

jtmcmc commented Dec 17, 2015

irb(main):041:0> foo = SortedSet.new
=> #<SortedSet: {}>
irb(main):042:0> foo << 1
=> #<SortedSet: {1}>
irb(main):043:0> foo << 6
=> #<SortedSet: {1, 6}>
irb(main):044:0> foo << 2
=> #<SortedSet: {1, 2, 6}>
irb(main):045:0> foo << 30
=> #<SortedSet: {1, 2, 6, 30}>
irb(main):046:0> foo
=> #<SortedSet: {1, 2, 6, 30}>
irb(main):047:0> foo.to_a.shift
=> 1
irb(main):048:0> foo
=> #<SortedSet: {2, 6, 30}>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment