Created
February 14, 2020 12:41
-
-
Save antonvolkoff/b4f781728c938aa29be7f5fde25dc500 to your computer and use it in GitHub Desktop.
Grouping by year
This file contains 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
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