The ALA thermometer in Ruby

October 5, 2026 · ALA · thermometer · Ruby

The first thermometer post took Spray’s example to the rung where every literal is at the top and every leaf is generic, and the follow-up climbed his next three steps in Elixir: build then run, instances with ports, and several kinds of connection. This is the same final rung in Ruby, as short as I could make it while scoring as high as the Ruby checklist and linter allow.

Ruby is the easy case for this one. Spray’s §1.6.5 is written with new and ports, and Ruby has classes and a ninety-line Foundation that gives them ports, so the code is close to his C#.

The application is the diagram

class Thermometer
  include Foundation::Ports
  include DomainAbstractions

  input(:tick, ProgrammingParadigms::Event) { @reader.input_port(:tick).send_event }
  input(:show, ProgrammingParadigms::Event) { @window.input_port(:show).send_event }

  def initialize(adc:)
    @reader = Reader.new(source: adc)
    @scale = OffsetAndScale.new(offset: -200, scale: 0.2)
    @filter = LowPassFilter.new(strength: 10, initial: 40.0)
    @sampler = SampleEvery.new(n: 10)
    @display = Display.new(label: "Temperature", units: "C", format: "%<label>s: %<value>.1f %<units>s",
                           empty: "Temperature: waiting for a reading")
    @window = Window.new(title: "Thermometer")

    @reader.wire_in(@scale).wire_in(@filter).wire_in(@sampler).wire_in(@display)
    @window.wire_to(@display, from: :contains, to: :render)
    @window.on(:frame) { @frame = _1 }
  end
end

Every number and word of the thermometer is in that constructor. The chain reads as the diagram because wire_in returns its target (§5.8.3), and the one cross-connection, the window containing the display, is a named instance, which is the only reason Spray gives for naming one (§3.6.2). Two inputs are the application’s own: tick, which the host sends on whatever clock it has, and show, which asks the window for a frame. Nothing here computes, branches, or holds data between calls. The frame landing in an on block is the one assignment, and it is Spray’s temperature connection (§1.6.6): where the dataflow lands for the host to read.

Three kinds of connection

Spray’s §1.6.6 point is that a real application composes more than one paradigm. The thermometer has three, and the Programming Paradigms layer defines each as a port object with one message:

The UI paradigm is eleven lines. Its whole job is to make the containment a wire the application draws, instead of a window that knows what a display is.

The domain knows no thermometer

Six classes, none of which says “temperature”. Reader reads a source on each event and sends what it read, staying quiet when the source is exhausted. OffsetAndScale is one line of arithmetic over its configuration. LowPassFilter keeps its running value as its own state, which is R4’s whole point: Spray chose abstraction over referential transparency here, because passing the memory in on every call “breaks an otherwise good abstraction” (§3.11.2). SampleEvery is the guard moved into the connection (§1.6.4): it passes every n-th value and swallows the rest, so the application has no if. Display shows what it was last sent, or the empty words it was given. Window renders its title over what it contains.

class SampleEvery
  include Foundation::Ports
  output :output, ProgrammingParadigms::DataFlow
  input(:input, ProgrammingParadigms::DataFlow) do |v|
    @count += 1
    emit(:output, v) if (@count % @n).zero?
  end
  def initialize(n:) = (@n, @count = n, 0)
end

The one default I wanted and didn’t take: Display could default its empty text, and R3 would have called it a product decision. It takes the words from the application instead.

What it scores, and what it draws

ala_lint default / --strict / --super-strict100 / 100 / 100, no finding at any tier
Rules met (of 10 checked)10
Non-blank lines, lib/ and bin/251
Tests4

The wiring coverage test lists every output of every part and finds none unwired, and the drawing is read from the built object graph, not the source:

flowchart LR
  screen[[Thermometer]]
  reader[reader: Reader]
  scale[scale: OffsetAndScale]
  filter[filter: LowPassFilter]
  sampler[sampler: SampleEvery]
  display[display: Display]
  window[window: Window]
  reader -- reading → input --> scale
  scale -- output → input --> filter
  filter -- output → input --> sampler
  sampler -- output → value --> display
  window -- contains → render --> display
  window -- frame --> screen

bin/run drives it over a warming sensor and shows the window after every tenth reading:

Thermometer | Temperature: 42.0 C
Thermometer | Temperature: 45.3 C
Thermometer | Temperature: 49.2 C

What the linter can’t score is whether this reads as the requirements. I think it does, and that is the honest limit of a score: the thermometer is small enough that R8 is a glance. The code is in ala_variants_ruby/thermometer.

← all posts