Posted almost 8 years ago. Visible to the public.

Time#utc, Time#gmt and Time#localtime are destructive methods

Calling Time#utc, Time#gmt or Time#localtime will not create a converted copy. Instead these methods modify the receiving Time object:

Copy
>> time = Time.now => Thu Aug 25 09:52:28 +0200 2011 >> time.utc => Thu Aug 25 07:52:28 UTC 2011 >> time => Thu Aug 25 07:52:28 UTC 2011

This can have unexpected side effects when other code is holding pointers to the Time object you are modifying. To be safe, call these methods on a clone of the Time object. You can clone a Ruby object by using #dup:

Copy
>> time = Time.now => Thu Aug 25 09:54:40 +0200 2011 >> time.dup.utc => Thu Aug 25 07:54:40 UTC 2011 >> time => Thu Aug 25 09:54:40 +0200 2011
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Henning Koch
Last edit:
about 7 years ago
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more