Fixing the warning Time#succ is obsolete; use time + 1

Posted . Visible to the public.

Chances are you're seeing the warning repeated a lot of times, maybe thousands of times. Here's how to reproduce the issue:

Example 1

# bad code
(Time.current .. Time.current + 1.hour).include?(Time.current)

# Use Range#cover? instead of Range#include? since the former does no typecasting into integers.
(Time.current .. Time.current + 1.hour).cover?(Time.current)

Example 2

# bad code
Post.where(:created_at => min_date.beginning_of_day .. max_date.end_of_day)

# Use 'BETWEEN x AND y'
Post.where(['posts.created_at BETWEEN ? AND ?', min_date.beginning_of_day, max_date.end_of_day])

Why?

Ruby 1.9.2 used to define a Time#succ method that returned a new Time object 1 second later than the previous one. This was used to iterate over time spans. That means for every second in the time range, Ruby 1.9.3 will show said deprecation warning.

Credits to Philipp Antar.

Martin Straub
Last edit
Martin Straub
License
Source code in this card is licensed under the MIT License.
Posted by Martin Straub to makandra dev (2014-12-18 15:51)