如何从ActiveSupport :: Duration对象中恢复整数 [英] How to recover an integer from an ActiveSupport::Duration object
本文介绍了如何从ActiveSupport :: Duration对象中恢复整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何恢复与ActiveSupport :: Duration对象相对应的整数?
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?
例如:
foo = 2.day
bar = foo.duration_integer # expected "2"
baz = foo.duration_type # expected "day" or "days"
推荐答案
您可以使用 parts
访问器是这样的。第一个元素是您称为 duration_type
的元素,最后一个是整数值:
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]
这篇关于如何从ActiveSupport :: Duration对象中恢复整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文