Read more

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

Deleted user #6
December 18, 2014Software engineer

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 online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
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 to makandra dev (2014-12-18 16:51)