Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 rails 日期减法
Date.new(2001,2,3) - Date.new(2001) #=> (33/1)
那/1说明什么?
/1
这是一个Rational:
Rational
(Date.new(2001,2,3) - Date.new(2001)).class #=> Rational
这就是通过以下方式显示 k 的方式inspect:
inspect
Rational(1) #=> (1/1)
如果你想要一个Integer,那么只需将其转换为一个:
Integer
(Date.new(2001,2,3) - Date.new(2001)).to_i #=> 33
这只是一个有理数:
您刚刚以合理的格式获得了两个日期之间的天数。