class

Unit::Length

Inherits Comparable < Unit::Formatter < Unit::Comparison < Unit::Arithmetic < Unit::Conversion < Reference < Object

Length measurement class with comprehensive unit support

Supports both metric and imperial length units with precise conversions using BigDecimal for accuracy. Meter is used as the base unit for all conversions to maintain consistency and precision.

Examples: Length.new(10.5, Length::Unit::Meter) Length.new(2.5, Length::Unit::Foot) Length.new(500, Length::Unit::Millimeter)

Constants

CONVERSION_FACTORS = {Length::Unit::Meter => BigDecimal.new("1"), Length::Unit::Centimeter => BigDecimal.new("0.01"), Length::Unit::Millimeter => BigDecimal.new("0.001"), Length::Unit::Kilometer => BigDecimal.new("1000"), Length::Unit::Inch => BigDecimal.new("0.0254"), Length::Unit::Foot => BigDecimal.new("0.3048"), Length::Unit::Yard => BigDecimal.new("0.9144"), Length::Unit::Mile => BigDecimal.new("1609.344")}

Conversion factors to meters (base unit)

All values are stored as BigDecimal for maximum precision Values are based on internationally accepted conversion standards

Constructors

from_json(string_or_io) : self

JSON deserialization

Source
from_yaml(string_or_io) : self

YAML deserialization

Source
new(value : Number, unit : Length::Unit)

Creates a new length with the given value and unit

Source
new(value : Number, unit_symbol : Symbol)

Creates a new length with the given value and unit symbol

Source

Class methods

base_unit

Returns the base unit for length measurements (meter)

Source
conversion_factor(unit : Length::Unit)

Returns the conversion factor for the given unit

Source
conversion_factor(unit_symbol : Symbol)

Returns the conversion factor for the given unit symbol

Source
metric_unit?(unit : Length::Unit)

Returns true if the given unit is metric

Source

Instance methods

inspect(io : IO) : Nil

Returns a detailed string representation for debugging

Source
symbol

Returns the symbol for this length's unit

Source
to_json(json : JSON::Builder) : Nil

JSON serialization support

Source
to_s(io : IO) : Nil

Returns a readable string representation of the measurement

Source
to_yaml(yaml : YAML::Nodes::Builder) : Nil

YAML serialization support

Source
unit

Unit of measurement

Source
unit_name(plural = false)

Returns the name of this length's unit

Source
value

Value stored as BigDecimal for precision

Source

Nested types