Read more

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

Martin Straub
December 18, 2014Software engineer at makandra GmbH

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?

Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more Show archive.org snapshot

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.

Posted by Martin Straub to makandra dev (2014-12-18 16:51)