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

Posted Almost 10 years ago. 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:

START OF BAD CODE

(Time.zone.now - 1.hour .. Time.zone.now + 1.hours).include? Time.zone.now

END OF BAD CODE

Ruby 1.9.2 used to define a Time#succ method Show archive.org snapshot 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.

The solution is either to use Range#cover? instead of Range#include? since the former does no typecasting into integers Show archive.org snapshot . The other solution may be to call to_date on time object, if that fits your needs.

Philipp Antar
Last edit
Almost 10 years ago
Posted by Philipp Antar to Distribusion IT (2014-04-09 14:03)