Skip to content

Add Measured::Temperature unit type#186

Closed
ntn wants to merge 2 commits intodynamic-conversionsfrom
add-temperature-type
Closed

Add Measured::Temperature unit type#186
ntn wants to merge 2 commits intodynamic-conversionsfrom
add-temperature-type

Conversation

@ntn
Copy link
Contributor

@ntn ntn commented Mar 6, 2026

Summary

Adds Measured::Temperature as the first dynamic unit type, demonstrating the proc-based conversion system from #185.

Includes three units:

  • Celsius (C) — base unit
  • Kelvin (K)celsius + 273.15
  • Fahrenheit (F)celsius * 9/5 + 32

Opt-in only

Temperature is not included in the default require "measured". Users opt in explicitly:

# In Gemfile
gem 'measured', require: ['measured', 'measured/temperature']

# Or manually
require 'measured/temperature'

This keeps the default gem lightweight and avoids loading dynamic conversion infrastructure for users who only need static units (length, weight, volume).

Usage

require 'measured/temperature'

temp = Measured::Temperature.new(100, :C)
temp.convert_to(:F)  # => 212 F
temp.convert_to(:K)  # => 373.15 K

Measured::Temperature.new(0, :K).convert_to(:C)  # => -273.15 C

Test plan

  • All temperature conversions (C↔K, C↔F, K↔F, identity, negatives, indirect paths)
  • Arithmetic and comparison across units
  • Existing unit tests (length, weight, volume) unaffected
  • require 'measured' alone does not load Temperature

@ntn ntn marked this pull request as draft March 6, 2026 05:49
@ntn ntn force-pushed the dynamic-conversions branch 4 times, most recently from bc22ec8 to 01b94d4 Compare March 9, 2026 21:52
@ntn ntn force-pushed the add-temperature-type branch from 7b07933 to 30330a9 Compare March 10, 2026 02:49
@ntn ntn force-pushed the dynamic-conversions branch from 01b94d4 to b39d147 Compare March 10, 2026 02:55
@ntn ntn force-pushed the add-temperature-type branch from 30330a9 to 248baee Compare March 10, 2026 02:56
@ntn ntn force-pushed the dynamic-conversions branch from b39d147 to 320ac2f Compare March 10, 2026 02:59
@ntn ntn force-pushed the add-temperature-type branch from 248baee to ef21a76 Compare March 10, 2026 03:00
@ntn ntn force-pushed the dynamic-conversions branch from 320ac2f to 378c206 Compare March 10, 2026 03:01
@ntn ntn force-pushed the add-temperature-type branch from ef21a76 to 4aac0a3 Compare March 10, 2026 03:01
ntn added 2 commits March 9, 2026 23:12
Add support for arbitrary conversion functions between units, enabling
non-proportional relationships like temperature.

Units can define conversions as proc pairs (forward/backward) instead of
ratios. FunctionalConversionTableBuilder builds conversion tables of
procs, composing them for indirect paths. UnitSystem auto-selects the
right builder based on whether any unit is functional.

Shared builder logic (cycle detection, caching, graph validation)
extracted into ConversionTableBuilderBase module.

UnitSystemBuilder gets cleaner DSL kwargs: convert_to:, forward:,
backward:, description: as an alternative to the value: hash format.

Includes Temperature as a proof-of-concept test fixture demonstrating
C, K, and F conversions.
Add Temperature as an opt-in unit type with Celsius, Kelvin, and
Fahrenheit. Not included in the default require - users opt in with:

  gem 'measured', require: ['measured', 'measured/temperature']

Or manually: require 'measured/temperature'
@ntn ntn force-pushed the dynamic-conversions branch from 378c206 to c6f7bb1 Compare March 10, 2026 03:18
@ntn ntn force-pushed the add-temperature-type branch from 4aac0a3 to b56e8c3 Compare March 10, 2026 03:18
@ntn ntn closed this Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant