garbagecollector
garbagecollector

Reputation: 3880

Forcing Ruby Time class to use ISO8601

How do you force Ruby's Time class to use ISO8601 when to_s is invoked?

Upvotes: 1

Views: 87

Answers (1)

infused
infused

Reputation: 24337

As @DumpHole mentioned, you can always override Time.to_s like so:

require 'time'    

class Time
  def to_s
    iso8601
  end
end

But, you may not want to override to_s for all instances of Time. Instead you can create a custom wrapper class and use that:

require 'time'

class Iso8601Time < Time
  def to_s
    iso8601
  end
end

Iso8601Time.now.to_s #=> "2015-04-17T17:35:13-07:00"

Upvotes: 3

Related Questions