From ff5c7c8c40ba24431ec5c9cdad23ddb9963d2c67 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 31 May 2007 16:37:09 +0000 Subject: Added Date#change (like Time#change) [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6910 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/core_ext/date/calculations.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'activesupport/lib/active_support/core_ext/date/calculations.rb') diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index b104e5271a..c522256aea 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -38,6 +38,20 @@ module ActiveSupport #:nodoc: d = d + options.delete(:days) if options[:days] d end + + # Returns a new Date where one or more of the elements have been changed according to the +options+ parameter. + # + # Examples: + # + # Date.new(2007, 5, 12).change(:day => 1) # => Date.new(2007, 5, 12) + # Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12) + def change(options) + ::Date.new( + options[:year] || self.year, + options[:month] || self.month, + options[:day] || options[:mday] || self.day # mday is deprecated + ) + end end end end -- cgit v1.2.3