Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Jim Cropcho

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

before? and after? - Ruby Time class mixin

The mixin below allows comparison between two Time objects using the more intuitive and natural-sounding before? and after? methods.

Some examples (using Rails' ActiveSupport Time extensions or (preferred) the 'units' gem):

   1  2.minutes.ago.after? Time.now # => false


   1  Time.now.before? 2.hours.from_now # => true


   1  module BeforeAndAfter
   2  
   3    LEFT_SIDE_LATER  = 1
   4    RIGHT_SIDE_LATER = -1
   5    
   6    def before?(input_time)
   7      (self <=> input_time) == RIGHT_SIDE_LATER
   8    end
   9    
  10    def after?(input_time)
  11      (self <=> input_time) == LEFT_SIDE_LATER
  12    end
  13  end
  14  
  15  Time.send :include , BeforeAndAfter
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS