From: Florian Gross Date: 2004-01-25T15:01:22+09:00 Subject: Re: [ANN] Ruby/Extensions v0.3 released Gavin Kistner wrote: > May I make the suggestion of adding in some convenience methods for the > Time class for manipulating dates, similar to what Javascript has? I think Time#year, #month, #week and #day could be useful. (#hour and #minute seem to be a bit extreme, but they might still be added.) What about adding these: (taken from good_time.rb ;)) # This makes Time act more like the Enumerable it really is # It also adds some Time travel methods class Time class Slice < Array; end extend Enumerable def succ self + 1 end def upto(other) self.to_i.upto(other.to_i) { |item| yield Time[item] } end def downto(other) self.to_i.downto(other.to_i) { |item| yield Time[item] } end class << self def [](*args) args = args.first .. (args.first + args.last) if args.length == 2 index = args.first unless index.is_a? Range at(index) else index.inject(Time::Slice.new) {|state, item| state << at(item)} end end def first; self[0]; end def last; self[2**31-1]; end def each first.upto(last) { |item| yield item } end alias :old_now :now def now; at(old_now.to_i + (@offset || 0)) end attr_accessor :offset def set(to) @offset ||= 0 @offset += to.to_i - Time.now.to_i now end alias :goto :set alias :travel :set def rewind; goto(Time.first); end # optimizations def max; last; end def min; first; end def include?(item); (first..last).include? item; end alias :member? :include? end end