class

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 unit
  • kilogram_per_liter (kg/L) - Equivalent to g/mL
  • gram_per_cubic_centimeter (g/cm³) - Equivalent to g/mL
  • kilogram_per_cubic_meter (kg/m³) - 0.001 g/mL

Imperial Units

  • pound_per_gallon (lb/gal) - 0.119826 g/mL
  • pound_per_cubic_foot (lb/ft³) - 0.0160185 g/mL
  • ounce_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 = {Density::Unit::GramPerMilliliter => BigDecimal.new("1"), Density::Unit::KilogramPerLiter => BigDecimal.new("1"), Density::Unit::GramPerCubicCentimeter => BigDecimal.new("1"), Density::Unit::KilogramPerCubicMeter => BigDecimal.new("0.001"), Density::Unit::PoundPerGallon => BigDecimal.new("0.119826427"), Density::Unit::PoundPerCubicFoot => BigDecimal.new("0.016018463"), Density::Unit::OuncePerCubicInch => BigDecimal.new("1.729994044")}

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

from_json(string_or_io) : self

JSON deserialization

Source
from_yaml(string_or_io) : self

YAML deserialization

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

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

Source
new(value : Number, unit_symbol : Symbol)

Creates a new density with the given value and unit symbol

Source

Class methods

base_unit

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
Source
conversion_factor(unit : Density::Unit)

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")
Source
conversion_factor(unit_symbol : Symbol)

Returns the conversion factor for the given unit symbol

Source
imperial_unit?(unit : Density::Unit)

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
Source
metric_unit?(unit : Density::Unit)

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
Source

Instance methods

inspect(io : IO) : Nil

Returns a detailed string representation for debugging

Source
symbol

Returns the symbol for this density'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

The unit of this density measurement

Source
unit_name(plural = false)

Returns the name of this density's unit

Source
value

The numeric value of the density measurement, stored as BigDecimal for precision

Source

Nested types