Code tagged with date

Create a Date object from a Date inspect

Posted by mqj 2 months ago
# Seeing things like "<Date: 4909275/2,0,2299161>" in logs gives no immediate 
# clue what that date is.  Now one can use Date.new_from_inspect to see what date the 
# inspect string represents.


>> Date.new_from_inspect('#<Date: 4909275/2,0,2299161>').to_s
=> "2008-06-20"
>> Date.new_from_inspect(Date.today.inspect) == Date.today
=> true


class Date
  def self.new_from_inspect(date_inspect_string)
    args = date_inspect_string.split(/[^\d]/).reject{|s| s.empty?}.map{|s| s.to_i}
    new!( Rational(*args.first(2)), *args.last(2) )
  end
end
Language Ruby / Tagged with date, inspect