Unit::Density
Inherits Comparable < Unit::Formatter < Unit::Comparison < Unit::Arithmetic < Unit::Conversion < Reference < Object
Density measurement class with comprehensive unit support
This class represents density measurements (mass per unit volume) and supports both metric and imperial units with precise conversions using BigDecimal for accuracy. Gram per milliliter (g/mL) is used as the base unit for all conversions to maintain consistency and precision.
Units Supported
Metric Units
gram_per_milliliter(g/mL) - Base unitkilogram_per_liter(kg/L) - Equivalent to g/mLgram_per_cubic_centimeter(g/cm³) - Equivalent to g/mLkilogram_per_cubic_meter(kg/m³) - 0.001 g/mL
Imperial Units
pound_per_gallon(lb/gal) - 0.119826 g/mLpound_per_cubic_foot(lb/ft³) - 0.0160185 g/mLounce_per_cubic_inch(oz/in³) - 1.72999 g/mL
Examples
# Creating densities
density = Unit::Density.new(1.0, :gram_per_milliliter)
water = Unit::Density.new(62.4, :pound_per_cubic_foot)
mercury = Unit::Density.new(13.534, :gram_per_cubic_centimeter)
# Converting between units
g_per_ml = density.convert_to(:gram_per_milliliter) # => 1.0 g/mL
kg_per_m3 = density.convert_to(:kilogram_per_cubic_meter) # => 1000 kg/m³
# Using for mass-volume conversions
weight = Unit::Weight.new(500, :gram)
volume = weight.to_volume(density) # => 500 mL
# Parsing from strings
parsed = Unit::Parser.parse("1.0 g/mL", Unit::Density)
Constants
Conversion factors to grams per milliliter (base unit)
All values are stored as BigDecimal for maximum precision Values are based on internationally accepted conversion standards
Constructors
Creates a new density measurement with the given value and unit.
# Using symbols (recommended)
density = Unit::Density.new(1.0, :gram_per_milliliter)
# Using enum values
density = Unit::Density.new(62.4, Unit::Density::Unit::PoundPerCubicFoot)
# Various numeric types supported
Unit::Density.new(1000, :kilogram_per_cubic_meter) # Int32
Unit::Density.new(0.92, :gram_per_cubic_centimeter) # Float64
Unit::Density.new(BigDecimal.new("1.5"), :g_per_ml) # BigDecimal
@param value The numeric value (any Number type) @param unit The unit as enum value or symbol @raise ArgumentError if value is NaN or infinite
Class methods
Returns the base unit for density measurements.
All density conversions go through gram per milliliter as the base unit to ensure consistency and minimize compound conversion errors.
Unit::Density.base_unit # => Unit::Density::Unit::GramPerMilliliter
Returns the conversion factor to grams per milliliter for the given unit.
This factor represents how many grams per milliliter are in one unit of the given type.
Unit::Density.conversion_factor(Unit::Density::Unit::KilogramPerLiter) # => BigDecimal("1")
Unit::Density.conversion_factor(:pound_per_gallon) # => BigDecimal("0.119826427")
Returns the conversion factor for the given unit symbol
Checks if the given unit is part of the imperial system.
Unit::Density.imperial_unit?(:pound_per_gallon) # => true
Unit::Density.imperial_unit?(:gram_per_milliliter) # => false
Checks if the given unit is part of the metric system.
Unit::Density.metric_unit?(:kilogram_per_liter) # => true
Unit::Density.metric_unit?(:pound_per_gallon) # => false