Skip to content

Instantly share code, notes, and snippets.

@felipeelias
Created October 3, 2011 18:23
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 felipeelias/1259830 to your computer and use it in GitHub Desktop.
Save felipeelias/1259830 to your computer and use it in GitHub Desktop.
Grouping posts by month concept
require 'date'
class Month
attr_accessor :year, :month
def initialize(year, month)
self.year = year
self.month = month
end
def to_s
Date.new(year, month).strftime('%B, %Y')
end
def hash
[year, month].hash
end
def eql?(other)
other.year == year && other.month == month
end
end
class Post
def month
Month.new(2011, 10)
end
end
posts = [Post.new, Post.new, Post.new]
puts posts.group_by(&:month).inspect
# => { October, 2011=>[#<Post:0x007ff7418ca2b8>, #<Post:0x007ff7418ca290>, #<Post:0x007ff7418ca268>] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment