Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 1.second.from_now and 1.seconds.from_now in Ruby's ActiveSupport library?

Tags:

ruby

I was curious about what the difference is between the two.

irb(main):001:0> require 'active_support/core_ext'
=> true
irb(main):002:0> 1.second.from_now == 1.seconds.from_now
=> false

They look the same to me

irb(main):003:0> p 1.second.from_now; p 1.seconds.from_now; nil
2013-06-14 17:50:28 +0530
2013-06-14 17:50:28 +0530
=> nil

And they both have the same class

irb(main):004:0> 1.second.from_now.class == 1.seconds.from_now.class
=> true
like image 957
wenderen Avatar asked Jun 14 '13 12:06

wenderen


1 Answers

Time elapses between both calls, that's why they are different:

Time.now == Time.now
#=> false

Time#to_f reveals that they are fractions apart:

a, b = 1.second.from_now, 1.second.from_now
a.to_f  #=> 1371213500.506212
b.to_f  #=> 1371213500.5062568

The call to second / seconds is identical:

1.second == 1.seconds
#=> true
like image 123
Stefan Avatar answered Sep 30 '22 18:09

Stefan