【问题标题】:How to recover an integer from an ActiveSupport::Duration object如何从 ActiveSupport::Duration 对象中恢复整数
【发布时间】:2015-02-21 15:10:36
【问题描述】:

如何恢复 ActiveSupport::Duration 对象对应的整数?

同时,是否可以恢复持续时间的类型?

示例:

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

【问题讨论】:

    标签: ruby-on-rails-4 duration activesupport


    【解决方案1】:

    您可以像这样使用parts 访问器。第一个元素是你所说的duration_type,最后一个是整数值:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      相关资源
      最近更新 更多