Created
October 3, 2011 18:23
-
-
Save felipeelias/1259830 to your computer and use it in GitHub Desktop.
Grouping posts by month concept
This file contains hidden or 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
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