Unit::Volume
Inherits Comparable < Unit::Formatter < Unit::Comparison < Unit::Arithmetic < Unit::Conversion < Reference < Object
Volume measurement class with comprehensive unit support
Supports both metric and US liquid volume units with precise conversions using BigDecimal for accuracy. Liter is used as the base unit for all conversions to maintain consistency and precision.
Focuses on US liquid measurements for cooking and recipe applications. Maintains high precision for accurate culinary conversions.
Examples: Volume.new(2.5, Volume::Unit::Cup) Volume.new(1.5, Volume::Unit::Liter) Volume.new(16, Volume::Unit::FluidOunce)
Constants
Conversion factors to liters (base unit)
All values are stored as BigDecimal for maximum precision Values are based on NIST Handbook 44 and US customary measurements
Constructors
Class methods
Returns the conversion factor for the given unit symbol
Returns true if the given unit is a US liquid measurement
Instance methods
Convert this volume to weight given a density value and unit
This is a convenience method that creates a Density object internally from the provided value and unit, then performs the conversion.
# Using density value and unit without creating Density object
volume = Unit::Volume.new(250, :milliliter)
flour_weight = volume.to_weight(0.593, :gram_per_milliliter) # ~148 g
# Different density units supported
honey_weight = volume.to_weight(1.42, :g_per_cc) # ~355 g
mercury_weight = volume.to_weight(13.534, :g_per_cc) # ~3.38 kg
@param density_value The numeric density value @param density_unit The unit of the density @return The weight that would occupy this volume at the given density @raise ArgumentError if density_value is zero or negative, or if density_unit is invalid
Convert this volume to weight given a density
This method calculates the mass that would occupy this volume at the given density, using the formula: mass = volume × density
# Using a Density object
volume = Unit::Volume.new(500, :milliliter)
water_density = Unit::Density.new(1.0, :gram_per_milliliter)
weight = volume.to_weight(water_density) # => 500 g
# Using density value and unit (creates Density internally)
weight = volume.to_weight(0.92, :gram_per_milliliter) # => 460 g
# With explicit naming for clarity
weight = volume.weight_given(water_density)
@param density The density of the material @return The weight that would occupy this volume at the given density @raise ArgumentError if density is zero or negative
Alias for to_weight with explicit naming for clarity (overload)
This method provides a more explicit name that can make code more readable, especially in complex expressions.
volume = Unit::Volume.new(250, :milliliter)
weight = volume.weight_given(0.593, :gram_per_milliliter)
@param density_value The numeric density value @param density_unit The unit of the density @return The weight that would occupy this volume at the given density
Alias for to_weight with explicit naming for clarity
This method provides a more explicit name that can make code more readable, especially in complex expressions.
volume = Unit::Volume.new(500, :milliliter)
weight = volume.weight_given(Unit::Density.new(1.0, :g_per_ml))
@param density The density of the material @return The weight that would occupy this volume at the given density