Read more

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

Henning Koch
August 25, 2011Software engineer at makandra GmbH

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

 >> 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
Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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 Show archive.org snapshot :

 >> 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
Posted by Henning Koch to makandra dev (2011-08-25 09:55)