问题
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