Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Florian Aßmann

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Try'n'Go: The Last Date

Extends Date class with a method that return the last Date of a month.
Date#last_of_month takes either a Fixnum or a Time as argument.

require 'date'

class Date
  def self.last_of_month( arg = Time.now )
    year = ( arg.is_a? Fixnum ) ? Time.now.year : arg.year
    mon  = ( arg.is_a? Fixnum ) ? arg : ( arg.mon rescue Time.now.mon )
    
    raise ArgumentError unless mon.between?( 1, 12 )

    begin; Date.new year, mon, mday ||= 31
    rescue ArgumentError; mday -= 1; retry
    end
  end
end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS