Today I Learned

hashrocket A Hashrocket project

Ruby usec

Two Time objects are almost never equal:

2.2.2 :001 > t = Time.now; t2 = Time.now
 => 2015-08-11 19:22:39 -0500
2.2.2 :002 > t == t2
 => false

This is true despite the fact that they were created in the same second of time, which we can verify by casting them to integers.

2.2.2 :003 > t.to_i
 => 1439338959
2.2.2 :004 > t2.to_i
 => 1439338959

The difference between these two times is in microseconds, which aren't displayed by default. Cast them as floats to see the difference.

2.2.2 :005 > t.to_f
 => 1439338959.099774
2.2.2 :006 > t2.to_f
 => 1439338959.099776

Another path to this information is Ruby's usec method, which returns just the microseconds.

2.2.2 :007 > t.usec
 => 99774
2.2.2 :008 > t2.usec
 => 99776

Documentation

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.