Sha256: 763e1cc65ada18b0f8244cdaed6186fdf6c0e17f080e29479c85d6d510207a50

Contents?: true

Size: 517 Bytes

Versions: 3

Compression:

Stored size: 517 Bytes

Contents

class YearMonth
  include Comparable

  attr_reader :year
  attr_reader :month

  def initialize(a, b = nil)
    if a.is_a? Time
      @year = a.year
      @month = a.month
    elsif b.nil?
      @year = (a / 100).to_i
      @month = a % 100
    else
      @year = a.to_i
      @month = b.to_i
    end
  end

  def <=>(b)
    to_i <=> b.to_i
  end

  def to_i
    @year * 12 + @month - 1
  end

  def to_s
    '%04d-%02d' % [@year, @month]
  end

  def eql?(b)
    to_i == b.to_i
  end

  def hash
    to_i
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
gitstats-rb-2.0.0 lib/gitstats/yearmonth.rb
gitstats-ruby-1.0.1 lib/gitstats/yearmonth.rb
gitstats-ruby-1.0.0 lib/gitstats/yearmonth.rb