How to recover an integer from an ActiveSupport::Duration object

戏子无情 提交于 2019-12-12 03:06:59

问题


How can I recover the integer corresponding to an ActiveSupport::Duration object?

At the same time, is it possible to recover the type of time duration?

exemple:

foo = 2.day
bar = foo.duration_integer  # expected "2"
baz = foo.duration_type     # expected "day" or "days"

回答1:


You can use the parts accessor like this. The first element is what you call the duration_type and the last one the integer value:

2.day.parts
=> [:days, 2]


来源:https://stackoverflow.com/questions/28647309/how-to-recover-an-integer-from-an-activesupportduration-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!