Module: ConverterChain
- Included in:
- Calcpace
- Defined in:
- lib/calcpace/converter_chain.rb
Overview
Module for chaining multiple unit conversions
This module allows performing multiple conversions in sequence, which is useful for complex unit transformations.
Instance Method Summary collapse
-
#convert_chain(value, conversions) ⇒ Float
Performs a chain of conversions on a value.
-
#convert_chain_with_description(value, conversions) ⇒ Hash
Performs a chain of conversions and returns a description.
Instance Method Details
#convert_chain(value, conversions) ⇒ Float
Performs a chain of conversions on a value
23 24 25 26 27 28 |
# File 'lib/calcpace/converter_chain.rb', line 23 def convert_chain(value, conversions) check_positive(value, 'Value') conversions.reduce(value) do |result, conversion| result * constant(conversion) end end |
#convert_chain_with_description(value, conversions) ⇒ Hash
Performs a chain of conversions and returns a description
39 40 41 42 43 44 45 46 |
# File 'lib/calcpace/converter_chain.rb', line 39 def convert_chain_with_description(value, conversions) initial_value = value result = convert_chain(value, conversions) conversion_names = conversions.join(' → ') description = "#{initial_value} → #{conversion_names} → #{result.round(4)}" { result: result, description: description } end |