Skip to content

Commit

Permalink
Date#freeze bug doesn't affect Ruby 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Aug 29, 2008
1 parent d4e668b commit e27e1f0
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions activesupport/lib/active_support/core_ext/date/behavior.rb
Expand Up @@ -12,16 +12,26 @@ def acts_like_date?
# the methods with one that caches the result in an instance variable.
# If a Date is frozen but the memoized method hasn't been called, the
# first call will result in a frozen object error since the memo
# instance variable is uninitialized. Work around by eagerly memoizing
# before freezing.
def freeze #:nodoc:
self.class.private_instance_methods(false).each do |m|
if m.to_s =~ /\A__\d+__\Z/
instance_variable_set(:"@#{m}", [send(m)])
# instance variable is uninitialized.
#
# Work around by eagerly memoizing before freezing.
#
# Ruby 1.9 uses a preinitialized instance variable so it's unaffected.
# This hack is as close as we can get to feature detection:
begin
::Date.today.freeze.jd
rescue => frozen_object_error
if frozen_object_error.message =~ /frozen/
def freeze #:nodoc:
self.class.private_instance_methods(false).each do |m|
if m.to_s =~ /\A__\d+__\Z/
instance_variable_set(:"@#{m}", [send(m)])
end
end

super
end
end

super
end
end
end
Expand Down

0 comments on commit e27e1f0

Please sign in to comment.