class method
self.build
Ruby on Rails 6.1.7.10
Since v5.2.8.1Signature
self.build(value)
Creates a new Duration from a seconds value that is converted to the individual parts:
ActiveSupport::Duration.build(31556952).parts # => {:years=>1} ActiveSupport::Duration.build(2716146).parts # => {:months=>1, :days=>1}
Parameters
-
valuereq
Source
# File activesupport/lib/active_support/duration.rb, line 183
def build(value)
unless value.is_a?(::Numeric)
raise TypeError, "can't build an #{self.name} from a #{value.class.name}"
end
parts = {}
remainder_sign = value <=> 0
remainder = value.round(9).abs
PARTS.each do |part|
unless part == :seconds
part_in_seconds = PARTS_IN_SECONDS[part]
parts[part] = remainder.div(part_in_seconds) * remainder_sign
remainder %= part_in_seconds
end
end unless value == 0
parts[:seconds] = remainder * remainder_sign
new(value, parts)
end
Defined in activesupport/lib/active_support/duration.rb line 183
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveSupport::Duration