Ruby 2.7 - Time#inspect separated from Time#to_s

Ruby 2.7 separated Time#to_s method from Time#inspect.

They were separated due to some troubles in the comparison of Time objects whose fractional parts were different.

For example, there are multiple time objects which are generated in a single HTTP request. They would be definitely different by some seconds. So assert_equal should fail.

But when we use assert_equal with inspect, it will return true as the fractional parts are ignored.

Before Ruby 2.7.0

Time.now.utc.to_s
=> "2019-12-10 13:25:22 UTC"

Time.now.utc.inspect
=> "2019-12-10 13:25:22 UTC"

(Time.now.utc + 0.1).to_s
=> "2019-12-10 13:25:22 UTC"

(Time.now.utc + 0.1).inspect
=> "2019-12-10 13:25:22 UTC"

In Ruby 2.7.0

Time.now.utc.to_s
=> "2019-12-10 13:25:22 UTC"

Time.now.utc.inspect
=> "2019-12-10 13:25:22.751418 UTC"

(Time.now.utc + 0.1).to_s
=> "2019-12-10 13:25:22 UTC"

(Time.now.utc + 0.1).inspect
=> "2019-12-10 13:25:22 17271867520919273597/22517998136852480000 UTC"

As seen above the fractional part is now not ignored when we use inspect.

Need help on your Ruby on Rails or React project?

Join Our Newsletter