Permalink
Browse files

Date#freeze bug doesn't affect Ruby 1.9

  • Loading branch information...
1 parent d4e668b commit e27e1f0308619795f766b2d2ef76aa80282cca01 @jeremy jeremy committed Aug 29, 2008
Showing with 18 additions and 8 deletions.
  1. +18 −8 activesupport/lib/active_support/core_ext/date/behavior.rb
@@ -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

0 comments on commit e27e1f0

Please sign in to comment.