Module: UnitConversions
- Defined in:
- lib/unit_conversions.rb
Overview
Pure-function helpers for converting between inches and feet/inches
notation (72 → 6′) and approximating decimal numbers as Rationals
with a bounded denominator. Used by the design-tool / room-layout
views where measurements need both decimal and "feet-inches" formats.
Class Method Summary collapse
-
.closest_rational(number, max_denominator = 1000) ⇒ Rational?
The closest rational approximation.
-
.inches_to_feetinches(inches, separator: ' ') ⇒ String?
The inches formatted as feet and inches.
Class Method Details
.closest_rational(number, max_denominator = 1000) ⇒ Rational?
Returns the closest rational approximation.
27 28 29 30 31 32 |
# File 'lib/unit_conversions.rb', line 27 def self.closest_rational(number, max_denominator = 1000) return if number.blank? # Convert the number to a rational approximation with a limited denominator Rational(number).rationalize(Rational(1, max_denominator)) end |
.inches_to_feetinches(inches, separator: ' ') ⇒ String?
Returns the inches formatted as feet and inches.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/unit_conversions.rb', line 8 def self.inches_to_feetinches(inches, separator: ' ') return nil if inches.nil? inches = inches.to_f s = [] ft = (inches / 12).truncate inches = (inches % 12) if ft.positive? ft = ActionController::Base.helpers.number_with_precision ft, precision: 0, strip_insignificant_zeros: true s << "#{ft}′" end if inches.positive? inches = ActionController::Base.helpers.number_with_precision inches, precision: 3, strip_insignificant_zeros: true s << "#{inches}″" end s.join(separator) end |