Ruby-on-rails 3.days.ago、2.hours.from_now 等没有 Rails?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6420371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
3.days.ago, 2.hours.from_now etc without Rails?
提问by d11wtq
Some book mentioned some gem to decorate numbers with #days, #megabytes, #minutesetc. 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.
有些书中提到的一些宝石与装饰数字#days,#megabytes,#minutes等等。这是只有在的ActiveSupport,还是有提供此功能用于(小)小宝石无轨宝石?我想将此功能用作 DSL 中的一小部分。
回答by Michael Kohl
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:
我不确定除了 之外是否还有另一个 gem 可用ActiveSupport,但是自己制作一个小版本真的很简单:
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_nowcan be implemented like agobut with + selfand weeks, hoursetc. like daysusing different constants.
from_now可以等来实现ago,但与+ self和weeks,hours等等days使用不同的常数。
回答by Jordan Running
ActiveSupporthas this functionality. It was originally part of Rails but can now be used separately.
ActiveSupport具有此功能。它最初是 Rails 的一部分,但现在可以单独使用。

