Module: TimeAgoInWords

Included in:
Time
Defined in:
lib/time_ago_in_words/methods.rb,
lib/time_ago_in_words/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#ago_in_wordsString

Convert elapsed seconds from Time.now to the current Time instance

into an English human readable text like '4 hours and 1 minute ago'

It is not well suited for dates older than 1 year

Returns:

  • (String)

    the humanized elapsed time in pairs Always returns a pair of words like:

    '23 days and 3 hours ago'
    '5 hours and 44 minutes ago'
    ' 1 minute and 11 seconds ago'
    

    Or just 1 when no hours/minutes/seconds available:

    '23 days ago'
    '17 hours ago'
    '1 minute ago'
    '1 second ago'
    

    Ẃhen 0 seconds or Time instance is a future date, returns empty string:

    ""
    


22
23
24
25
26
27
28
29
30
# File 'lib/time_ago_in_words/methods.rb', line 22

def ago_in_words
  return 'a very very long time ago' if self.year < 1800
  secs = Time.now - self
  return 'just now' if secs > -1 && secs < 1
  return '' if secs <= -1
  pair = ago_in_words_pair(secs)
  ary = ago_in_words_singularize(pair)
  ary.size == 0 ? '' : ary.join(' and ') << ' ago'
end