Skip to content

Instantly share code, notes, and snippets.

@antonvolkoff
Created February 14, 2020 12:41
Show Gist options
  • Save antonvolkoff/b4f781728c938aa29be7f5fde25dc500 to your computer and use it in GitHub Desktop.
Save antonvolkoff/b4f781728c938aa29be7f5fde25dc500 to your computer and use it in GitHub Desktop.
Grouping by year
module ShitGrouper
def self.group(array)
array.group_by(&:year)
end
def self.ungroup(hash)
hash.values.flatten
end
end
class Shit
attr_reader :year, :weight
def initialize(year, weight)
@year = year
@weight = weight
end
end
shits = 10.times.map do |n|
Shit.new(2000 + n, rand(10..100))
end
grouped_shit = ShitGrouper.group(shits)
ungrouped_shit = ShitGrouper.ungroup(grouped_shit)
if ungrouped_shit == shits
puts "I'm still the same shit as I used to be"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment