本文介绍了3.days.ago,2.hours.from_now等没有Rails?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些书提到了一些用 #days #megabytes #minutes 等。这只是在ActiveSupport中,还是有一个小的宝石,提供此功能用于(小)非轨道宝石?我想在一个小小的宝石中使用这个功能作为DSL的一部分。

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.

推荐答案

我不确定是否有另一个除了 ActiveSupport 之外,还可以使用gem,但你自己做一个小版本真的很简单:

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 可以像但以 + self 小时等,如使用不同的常量。

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

这篇关于3.days.ago,2.hours.from_now等没有Rails?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 22:41