CaseA case/switch expression: match a subject against a series of conditions and answer the first matching arm's value.
Built with Object case: - inside the case block, self is the Case, so arms read as bare .when:cond do:... sends. Conditions are compared with the match operator ~ (equality by default; e.g. a NumberRange matches containment), arms are tried in order, and the first match wins by throwing its result - so default: must come last: it throws unconditionally, and no arm written after it is ever tried. When nothing matches and there is no default:, the case answers nil.
7.case:{ .when:(1..5) do:{ 'low' }; .when:(5..10) do:{ 'high' }; .default:{ 'out' } } "* -> high
Run a case block against obj and answer the first matching arm's result.
The block is evaluated with obj as its argument and a fresh Case as self; a matching arm throws its result, which is caught and answered here. Prefer the obj.case:{ ... } form.
The catch-all arm: unconditionally answer the block's value. Must be the last arm - it always matches, so no arm after it is tried.
Bind the subject the arms will be matched against.
A match arm: when cond ~ subject, the block's value becomes the case result (the block receives the subject).
The result is delivered by throwing, so the first matching arm wins and later arms are never evaluated.
'q'.case:{ .when:'q' do:{ |c| c + '!' }; .default:{ 'no' } } "* -> q!