Rails如何在数字上实现“日期魔术”机制?

时间:2013-06-21 03:05:41

标签: ruby-on-rails

在Rails中(但不是普通的Ruby)可以说:

t = 2.days
t.inspect 

=> “2天”

t.to_i

=> 172800

看起来Rails修补了Fixnum,因此它保存了间隔和检查字符串所代表的秒数和描述。这基本上是如何运作的?

1 个答案:

答案 0 :(得分:2)

ActiveSupport具有基于Ruby类的核心扩展,例如字符串,对象和数字,这使得这成为可能。

有关导轨指南的更多信息:http://edgeguides.rubyonrails.org/active_support_core_extensions.html#time

ActiveSupport正在为数字添加“天数”,并进行计算以提供

代码:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/numeric/time.rb

这是非常基本的:

  • 1天是24小时
  • 1小时是60分钟