Post Image
By Matt GaidicaJune 8, 2012In Uncategorized

A Better time_ago_in_words for Ruby on Rails

def time_ago time, append = ' ago'
  return time_ago_in_words(time).gsub(/about|less than|almost|over/, '').strip << append
end

I love the time_ago_in_words method that comes bundled with rails, but it often produces a pretty lengthy string that doesn’t fit on narrow columns, or mobile views. This is a simple fix I use, placed in “helpers/application_helper.rb”.

time_ago(Time.now)
#a minute ago

time_ago(5.minutes.from_now)
#5 minutes ago

time_ago(Time.now - 20.years, '!')
#20 years!
svgMaking files public in Amazon S3 and the owner problem
svg
svgrspec_helper.rb for Rspec in Ruby