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

前端 未结 3 732
天命终不由人
天命终不由人 2021-02-11 18:42

Some book mentioned some gem to decorate numbers with #days, #megabytes, #minutes etc. Is this only in ActiveSupport, or is there a smalle

相关标签:
3条回答
  • 2021-02-11 19:14

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

    0 讨论(0)
  • 2021-02-11 19:16

    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.

    0 讨论(0)
  • 2021-02-11 19:17

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

    0 讨论(0)
提交回复
热议问题