有人可以解释这个奇怪的Rails ActiveSupport :: Duration行为吗?

时间:2015-09-01 18:05:50

标签: activesupport

我正在玩游戏时间(和postgres间隔,长篇故事),我得到了这个。

[6] pry(main)> 30.days.days
=> 2592000 days

为什么它不会返回30 days?那不是很有意义吗?我错过了什么......?

1 个答案:

答案 0 :(得分:0)

You can see the implementation of the days method here and here.

  def days
    AS::Duration.new(self * 24*60*60, [[:days, self]])
  end

So, everytime you call .days it will multiply the current value with 24*60*60 = 86400 and then return the value.

So:

[61] pry(main)> 30.days
=> 30 days
[62] pry(main)> 30.days.days
=> 2592000 days
[63] pry(main)> 30.days.days.days
=> 223948800000 days

So, if you want 30 days, just use 30.days which makes sense!