The coffee maker in Ruby: Spray's diagram, wired as objects

October 5, 2026 · ALA · coffee-maker · Ruby · R11

The Elixir coffee maker kept Spray’s three abstractions pure and nested them in a LiveView, and its hand walk deducted for one thing: the user stories were ifs and assignments in the application, Spray’s own six lines of hand-compiled C. His diagram (§2.9.3) has no ifs. “Start brewing on button, water and pot” is an AND gate wired to three outputs; the brewing state is a state-machine instance; “the boiler ran dry” is an edge on a dataflow line. That form wants objects with ports, and Ruby has them. So this version builds the diagram, not the compiled lines, and the application has no logic at all.

The three abstractions

Spray’s §2.9.1 description of each is the whole specification, and each class’s header comment restates it. A boiler can be turned on or off, tells you when it is empty, and stops the flow with its valve; it turns its own heater off when it runs dry or the valve is open, and that rule is the boiler’s:

class Boiler
  include Foundation::Ports
  output :empty, ProgrammingParadigms::DataFlow, many: true
  output :heater, ProgrammingParadigms::DataFlow
  output :valve, ProgrammingParadigms::DataFlow

  input(:sense, ProgrammingParadigms::DataFlow) do |reading|
    dry = reading.boiler == :empty
    was, @dry = @dry, dry
    emit(:empty, dry) unless dry == was
    drive
  end
  input(:on, ProgrammingParadigms::DataFlow) { |v| @on = v; drive }
  input(:open_valve, ProgrammingParadigms::DataFlow) { |v| @open = v; drive }

  private

  def drive
    emit(:valve, @open ? :open : :closed)
    emit(:heater, @on && !@dry && !@open ? :on : :off)
  end
end

The warmer plate tells you whether a pot is on it and whether an empty pot is on it, and runs its own heater only under a pot with something in it. The front panel sends a button event and controls a lamp. None of the three mentions coffee, brewing, or each other.

The hardware boundary is data, as in the Elixir version: a SensorReading with the three sensors, a HardwareCommand with the four actuators. A cycle pushes one reading into the three abstractions’ sense inputs and answers with the command their outputs landed in.

The logic abstractions

Spray’s key says what the diagram is drawn with: live dataflow, events, and a simple state machine (§2.9.2). In Ruby those are four small domain classes and one paradigm. Gate passes an event through while its two level inputs are true. Not and Equals send their output whenever it changes. RisingEdge sends an event on each change from false to true. StateMachine, in the paradigms layer, takes a table of [from, event, to], gives itself one event input per event, and sends its state on a dataflow output whenever it changes. The whole set is sixty lines, and none of it is about coffee either.

The application is the diagram

class CoffeeMaker
  include Foundation::Ports
  include DomainAbstractions

  FLOW = [ [ :idle, :brew, :brewing ], [ :brewing, :dry, :brewed ], [ :brewed, :replaced, :idle ] ].freeze

  input(:cycle, ProgrammingParadigms::DataFlow) do |reading|
    [ @boiler, @plate, @ui ].each { _1.input_port(:sense).push(reading) }
    @command
  end

  def initialize
    @command = Foundation::HardwareCommand.off
    @ui, @boiler, @plate = UserInterface.new, Boiler.new, WarmerPlate.new
    @state = ProgrammingParadigms::StateMachine.new(transitions: FLOW, start: :idle)
    @start, @not_empty, @pot_off = Gate.new, Not.new, Not.new
    @ran_dry, @pot_replaced = RisingEdge.new, RisingEdge.new
    @brewing, @brewed = Equals.new(:brewing), Equals.new(:brewed)

    # pressing the button starts brewing, provided the boiler has water and the pot is on the plate
    @ui.wire_to(@start, from: :button, to: :fire)
    @boiler.wire_to(@not_empty, from: :empty, to: :in)
    @not_empty.wire_to(@start, from: :out, to: :a)
    @plate.wire_to(@start, from: :pot_on_plate, to: :b)
    @start.wire_to(@state, from: :passed, to: :brew)
    # while brewing, the boiler is on
    @state.wire_to(@brewing, from: :state, to: :in)
    @brewing.wire_to(@boiler, from: :out, to: :on)
    # the valve opens whenever the pot is off the plate
    @plate.wire_to(@pot_off, from: :pot_on_plate, to: :in)
    @pot_off.wire_to(@boiler, from: :out, to: :open_valve)
    # the boiler running dry means brewed, and brewed lights the lamp
    @boiler.wire_to(@ran_dry, from: :empty, to: :in)
    @ran_dry.wire_to(@state, from: :rose, to: :dry)
    @state.wire_to(@brewed, from: :state, to: :in)
    @brewed.wire_to(@ui, from: :out, to: :light)
    # an empty pot put back means idle again
    @plate.wire_to(@pot_replaced, from: :pot_empty, to: :in)
    @pot_replaced.wire_to(@state, from: :rose, to: :replaced)
    # the actuators
    @boiler.on(:heater) { @command = @command.with(boiler_heater: _1) }
    @boiler.on(:valve) { @command = @command.with(relief_valve: _1) }
    @plate.on(:heater) { @command = @command.with(warmer_heater: _1) }
    @ui.on(:indicator) { @command = @command.with(indicator: _1) }
  end
end

Read it against Spray’s walk through the user stories (§2.9.3) and each comment is one of his sentences. There is no if. The AND gate holds the brew condition; the state machine holds the history; the two rising edges are the “becomes empty” and “is replaced empty” events his hand-compiled lines wrote as empty && !prevEmpty. The on blocks at the end are the actuator landings, Spray’s temperature connection, with the command built up as an immutable value (Data#with) because R2 wants values, not a mutable struct the sinks share.

Spray’s Poll reads all three abstractions before running the lines. With push dataflow the application gets the same ordering by sensing the boiler and the plate before the button, so the gate sees this cycle’s levels when the press arrives.

Drawn from the built graph

flowchart LR
  screen[[CoffeeMaker]]
  ui[ui: UserInterface]
  boiler[boiler: Boiler]
  plate[plate: WarmerPlate]
  state[state: StateMachine]
  start[start: Gate]
  boiler_has_water[boiler_has_water: Not]
  pot_off[pot_off: Not]
  ran_dry[ran_dry: RisingEdge]
  pot_replaced[pot_replaced: RisingEdge]
  brewing[brewing: Equals]
  brewed[brewed: Equals]
  ui -- button → fire --> start
  ui -- indicator --> screen
  boiler -- empty → in --> boiler_has_water
  boiler -- empty → in --> ran_dry
  boiler -- heater --> screen
  boiler -- valve --> screen
  plate -- pot_on_plate → b --> start
  plate -- pot_on_plate → in --> pot_off
  plate -- pot_empty → in --> pot_replaced
  plate -- heater --> screen
  state -- state → in --> brewing
  state -- state → in --> brewed
  start -- passed → brew --> state
  boiler_has_water -- out → a --> start
  pot_off -- out → open_valve --> boiler
  ran_dry -- rose → dry --> state
  pot_replaced -- rose → replaced --> state
  brewing -- out → on --> boiler
  brewed -- out → light --> ui

That is Figure 22 of his site, read off the running objects. A coverage test lists every output of every part and finds none unwired.

Tests and the simulator

Martin’s acceptance scenarios are readings in and commands out, with no device to mock:

def test_lifting_the_pot_opens_the_valve_and_the_boiler_stops_heating
  cycle
  cycle(button: :pushed)
  command = cycle(warmer_plate: :warmer_empty)
  assert_equal :open, command.relief_valve
  assert_equal :off, command.boiler_heater
end

A SimulatedHardware in the Foundation models water boiling into the pot, so bin/run brews one:

 0               idle     boiler off valve closed warmer off lamp off water 3 pot 0
 1 press_button  brewing  boiler on  valve closed warmer off lamp off water 2 pot 1
 2               brewing  boiler on  valve closed warmer on  lamp off water 1 pot 2
 3               brewing  boiler on  valve closed warmer on  lamp off water 0 pot 3
 4 lift_pot      brewed   boiler off valve open   warmer off lamp on  water 0 pot 3
 5 replace_pot   brewed   boiler off valve closed warmer on  lamp on  water 0 pot 3
 9 replace_pot   idle     boiler off valve closed warmer off lamp off water 0 pot 0

The simulator caught a modelling slip the tests hadn’t: the plate’s pot_empty first meant “no full pot”, so lifting a full pot while brewed read as replacing an empty one. Spray’s sensor has three states for a reason.

What it scores

Elixir (LiveView)Ruby
ala_lint default / --strict / --super-strict99 / 97 / 77100 / 100 / 100
Full checklist, by hand95no deduction found
Logic in the applicationsix lines of ifsnone
Non-blank lines—375
Tests3511

The Elixir version’s deductions were all in its top layer; this one has nothing there to deduct. It isn’t that Ruby is better at ALA than Elixir. It’s that Spray’s diagram is an object graph, and a language with mutable objects and ports draws it directly, where the Elixir version chose to stay pure and compile the diagram to lines, as Spray’s C did. Two things Ruby made me learn on the way: an output port is the instance variable of its name, so a class can’t keep state under that name too (the boiler holds @dry and sends on empty), and instances fed in one cycle see each other’s outputs in wiring order, which is what makes the sense order matter.

No Rails host yet; the model is plain Ruby and would sit under a controller the way the Elixir one sat under a LiveView, a reading per request and a command back. The code is in ala_variants_ruby/coffee_maker.

← all posts