Duplicating the options hash in Date#advance to prevent modification of the original [#1133 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
This commit is contained in:
Jay Pignata
2009-08-31 13:01:34 -05:00
committed by Joshua Peek
parent 49342d1745
commit 49c4a79e59
2 changed files with 7 additions and 0 deletions

View File

@@ -92,6 +92,7 @@ module ActiveSupport #:nodoc:
# Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
# any of these keys: <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>.
def advance(options)
options = options.dup
d = self
d = d >> options.delete(:years) * 12 if options[:years]
d = d >> options.delete(:months) if options[:months]

View File

@@ -250,6 +250,12 @@ class DateExtCalculationsTest < Test::Unit::TestCase
Time.zone_default = nil
end
def test_date_advance_should_not_change_passed_options_hash
options = { :years => 3, :months => 11, :days => 2 }
Date.new(2005,2,28).advance(options)
assert_equal({ :years => 3, :months => 11, :days => 2 }, options)
end
protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz