Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

TimeZone requires preloading before #freeze #1977

Closed
wants to merge 2 commits into from

6 participants

@jfelchner

'Can't modify frozen object' when calling #freeze after instantiation

Although I did add tests to this which should have failed, for some reason they didn't. If someone with more knowledge of Rails dependencies could help me out I would appreciate it.

The issue this fixes is here: #1966

There is a 100% repeatable (on the 3 MacBooks I tried) bug wherein executing the following:

rails new foo
cd foo
bundle install
rails console

And within IRB:

ActiveSupport::TimeZone.new('Alaska').freeze

Raises a Can't modify a frozen object RuntimeError.

Although I can get this error to occur every time, I cannot get it to be reproduced in a test.

I hope this at least puts everyone on the right track to tracking down this problem.

activesupport/lib/active_support/values/time_zone.rb
@@ -289,6 +289,12 @@ module ActiveSupport
Time.now.utc.in_time_zone(self)
end
+ # Preload information prior to freezing
+ def freeze
+ tzinfo; utc_offset;
@dasch
dasch added a note

Better split this line into two, and get rid of the semicolons.

@sikachu Collaborator
sikachu added a note

I'm agree. Can you amend your patch and force-push to your branch?

Sorry guys, was on vacation. I can certainly do that. I'll get it in today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
jfelchner added some commits
@jfelchner jfelchner TimeZone requires preloading before #freeze
'Can't modify frozen object' when calling #freeze after instantiation
12ad541
@jfelchner jfelchner Split onto two lines
cb50b61
@jfelchner

@sikachu Sorry for the delay. Hope this meets your approval. Thanks again for taking a look at this!

@arunagw
Collaborator

Not sure about the orignal issue. But what's going on about this PR?

@isaacsanders

Is this still an issue?

@jfelchner

This was previously an issue for me but I haven't been hacking on timezones in a while.

I just tried it again and cannot reproduce. I'll close it for now.

@jfelchner jfelchner closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 31, 2011
  1. @jfelchner

    TimeZone requires preloading before #freeze

    jfelchner authored
    'Can't modify frozen object' when calling #freeze after instantiation
  2. @jfelchner

    Split onto two lines

    jfelchner authored
This page is out of date. Refresh to see the latest.
View
8 activesupport/lib/active_support/values/time_zone.rb
@@ -289,6 +289,14 @@ def now
Time.now.utc.in_time_zone(self)
end
+ # Preload information prior to freezing
+ def freeze
+ tzinfo
+ utc_offset
+
+ super
+ end
+
# Return the current date in this time zone.
def today
tzinfo.now.to_date
View
17 activesupport/test/time_zone_test.rb
@@ -294,6 +294,23 @@ def test_new
assert_equal ActiveSupport::TimeZone["Central Time (US & Canada)"], ActiveSupport::TimeZone.new("Central Time (US & Canada)")
end
+ def test_freeze
+ @tz = ActiveSupport::TimeZone.new("Arizona")
+ @tz.freeze
+
+ assert @tz.frozen?
+ end
+
+ def test_freeze_preloads_instance_variables
+ @tz = ActiveSupport::TimeZone.new('Alaska')
+
+ assert_nothing_raised do
+ @tz.freeze
+ @tz.utc_offset
+ @tz.tzinfo
+ end
+ end
+
def test_us_zones
assert ActiveSupport::TimeZone.us_zones.include?(ActiveSupport::TimeZone["Hawaii"])
assert !ActiveSupport::TimeZone.us_zones.include?(ActiveSupport::TimeZone["Kuala Lumpur"])
Something went wrong with that request. Please try again.