Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
require 'date' require 'time' class Time def to_s(format = :default) case DATE_FORMATS[format] when Proc then DATE_FORMATS[format].call(self) when String then strftime(DATE_FORMATS[format]).strip else to_default_s end end end # # Custom time formats # # >> time.to_s(:stamp) # => 5:23PM # # >> time.to_s(:short) # => Dec 3, 2006 # # >> time.to_s(:relative) # => today # Time::DATE_FORMATS[:stamp] = '%l:%M%p' Time::DATE_FORMATS[:short] = '%b %e, %Y' Time::DATE_FORMATS[:relative] = Proc.new do |date| date = Date.parse(date.to_s, true) days = (date - Date.today).to_i case true when days >= 0 && days < 1 then 'today' when days >= 1 && days < 2 then 'tomorrow' when days >= -1 && days < 0 then 'yesterday' when days.abs < 7 then "last " << date.strftime('%A').downcase when days.abs < 182 then date.strftime('%A, %B %e') else date.strftime('%A, %B %e, %Y') end end
This paste will be private.
From the Design Piracy series on my blog: