Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3.days.ago, 2.hours.from_now etc without Rails?

Some book mentioned some gem to decorate numbers with #days, #megabytes, #minutes etc. Is this only in ActiveSupport, or is there a smaller gem that provides this functionality for use in (small) non-rails gems? I want to use this functionality as part of a DSL in a tiny little gem.

like image 267
d11wtq Avatar asked Jun 21 '11 04:06

d11wtq


3 Answers

I'm not sure if there's another gem available besides ActiveSupport, but it would be really straight-forward to make a small version yourself:

class Fixnum
  SECONDS_IN_DAY = 24 * 60 * 60

  def days
    self * SECONDS_IN_DAY
  end

  def ago
    Time.now - self
  end
end

3.days.ago #=> 2011-06-18 08:45:29 0200

from_now can be implemented like ago but with + self and weeks, hours etc. like days using different constants.

like image 84
Michael Kohl Avatar answered Nov 19 '22 09:11

Michael Kohl


ActiveSupport has this functionality. It was originally part of Rails but can now be used separately.

like image 45
Jordan Running Avatar answered Nov 19 '22 08:11

Jordan Running


I found: https://github.com/kylewlacy/timerizer

like image 26
Erik Avatar answered Nov 19 '22 07:11

Erik