Funicular: web apps in Ruby
A funicular is a cable railway that pulls its cars up a steep slope – the old Neapolitan song Funiculì, Funiculà is about the one on Mount Vesuvius. Funicular is also a framework by HASUMI Hitoshi, who made PicoRuby: with it you build apps for the browser in Ruby instead of JavaScript. It runs on PicoRuby.wasm from lesson 90, which is small: CRuby with its standard library takes 31 MB, PicoRuby.wasm 2.7 MB.
A Funicular app is made of components: Ruby classes whose render method says what the page looks like. Run the cell: below it a fresh PicoRuby starts in a window of its own and shows the app. CRuby reads your code first, without a browser – so a mistake is explained here, as in every lesson.
class HelloFunicular < Funicular::Component
def render
div do
h1 { "Chunky bacon!" }
p { "Ruby drew this page, right here in your browser." }
p(style: "color: #bd2d00") { "#{RUBY_ENGINE} #{RUBY_VERSION}" }
end
end
end
Funicular.start(HelloFunicular, container: "app")Inside render, every HTML tag is a method: div, h1, p, button, ul, li, input … What the block returns goes inside the tag, and blocks inside blocks make tags inside tags. Attributes are keyword arguments: p(style: "color: red"), div(class: "box"). Careful: in a component p makes a paragraph – it prints nothing. Funicular.start puts the component into the element of the page whose id is app.
State, and a click
A page that only shows something could be plain HTML. An app changes. A component keeps what can change in its state: a Hash that initialize_state returns, read with state[:slices]. onclick: :more says: on a click, call the method more. And that method never touches the page – it calls patch with the new values. Click the buttons:
class BaconCounter < Funicular::Component
def initialize_state
{ slices: 0 }
end
def more(event)
patch(slices: state[:slices] + 1)
end
def less(event)
patch(slices: state[:slices] - 1) if state[:slices] > 0
end
def render
div do
h2 { "Bacon slices: #{state[:slices]}" }
p { "🥓" * state[:slices] }
button(onclick: :less) { "–1" }
button(onclick: :more) { "+1" }
end
end
end
Funicular.start(BaconCounter, container: "app")What the red frames show
patch merges the new values into the state and calls render again. render describes the whole component anew – but Funicular does not throw the page away. render builds a virtual DOM: Ruby objects that stand for the tags. The Differ compares the new tree with the last one and lists only the differences; the Patcher applies just those to the real page. The red frames mark what the Patcher changed, and the log under the app counts it: a click on +1 changes the two texts – every other node stays as it was. React works this way too; Funicular does it in Ruby, in about 10,000 lines.
Typing works the same way. oninput calls the method on every key, and event.target[:value] is what is in the box now. Type your name:
class FoxGreeter < Funicular::Component
def initialize_state
{ name: "" }
end
def typed(event)
patch(name: event.target[:value].to_s)
end
def render
name = state[:name]
div do
input(placeholder: "Your name", value: name, oninput: :typed)
if name.empty?
p { "🦊 Who's there?" }
else
h2 { "Hello, #{name}!" }
p { "#{name.length} letters – backwards: #{name.reverse.downcase}" }
end
end
end
end
Funicular.start(FoxGreeter, container: "app")The box stays put while you type: the Patcher leaves that element alone and changes only the texts below it. That is why the cursor does not jump.
Lists, and key
A list is a loop in render: each over an Array in the state, one li per item. Give each li a key – a value that belongs to its item, like an id. Then delete the first item: thanks to the keys, the Differ sees that one item went and the rest stayed, and removes exactly one element. Without keys it would compare by position and rewrite every item below.
Two more things are new. form(onsubmit: :add) catches Enter as well as the button, and event.preventDefault stops the browser from sending the form off and reloading the page. And the handlers of ☐ and ✕ are lambdas, made by toggle(id) and remove(id) – each knows which item it belongs to. The state is never changed in place: map, reject and + make a new Array, and patch gets it.
class FoxTodo < Funicular::Component
def initialize_state
{ items: [{ id: 1, text: "Fry bacon", done: true },
{ id: 2, text: "Learn Ruby", done: false }],
draft: "", next_id: 3 }
end
def typed(event)
patch(draft: event.target[:value].to_s)
end
def add(event)
event.preventDefault
text = state[:draft].strip
return if text.empty?
item = { id: state[:next_id], text: text, done: false }
patch(items: state[:items] + [item], draft: "", next_id: state[:next_id] + 1)
end
# a click handler for one item: a lambda that knows its id
def toggle(id)
lambda do
items = state[:items].map do |i|
i[:id] == id ? i.merge(done: !i[:done]) : i
end
patch(items: items)
end
end
def remove(id)
-> { patch(items: state[:items].reject { |i| i[:id] == id }) }
end
def render
left = state[:items].select { |i| !i[:done] }.size
div do
form(onsubmit: :add) do
input(value: state[:draft], placeholder: "What next?", oninput: :typed)
button(type: "submit") { "Add" }
end
ul do
state[:items].each do |item|
li(key: item[:id]) do
button(onclick: toggle(item[:id])) { item[:done] ? "☑" : "☐" }
span(style: item[:done] ? "text-decoration: line-through" : "") { item[:text] }
button(onclick: remove(item[:id])) { "✕" }
end
end
end
p { "Still to do: #{left}" }
end
end
end
Funicular.start(FoxTodo, container: "app")Components in components
A big app is built from small components. component(BaconStar, number: n, …) puts one into render and hands it props: values it reads with props[:number] and does not change. How does a star tell the rating that it was clicked? The rating hands it a lambda as a prop, on_pick, and the star calls it. Data flows down as props, events flow up through lambdas – the same pattern as in React.
class BaconStar < Funicular::Component
def render
button(onclick: -> { props[:on_pick].call(props[:number]) }) do
props[:lit] ? "★" : "☆"
end
end
end
class BaconRating < Funicular::Component
WORDS = ["Pick a star", "Hmm", "OK", "Good", "Very good", "Chunky bacon!"]
def initialize_state
{ stars: 0 }
end
def render
div do
h3 { "How much do you like bacon?" }
(1..5).each do |n|
component(BaconStar, number: n, lit: n <= state[:stars],
on_pick: ->(picked) { patch(stars: picked) })
end
p { WORDS[state[:stars]] }
end
end
end
Funicular.start(BaconRating, container: "app")next_light(event): it switches the light on with patch – red, then green, then yellow, then red again. Then give the button onclick: :next_light. Run the cell and click through the colours.class TrafficLight < Funicular::Component
COLORS = { "red" => "#e53935", "yellow" => "#fdd835", "green" => "#43a047" }
def initialize_state
{ light: "red" }
end
# next_light: red -> green -> yellow -> red
def lamp(color)
lit = state[:light] == color
div(style: "width: 46px; height: 46px; margin: 6px; border-radius: 50%; " \
"background: #{lit ? COLORS[color] : "#444"}")
end
def render
div do
div(style: "display: inline-block; padding: 4px; background: #222; border-radius: 12px") do
lamp("red")
lamp("yellow")
lamp("green")
end
p { "Light: #{state[:light]}" }
button { "Next" }
end
end
end
Funicular.start(TrafficLight, container: "app")Funicular on Rails
Here each app lived alone in its window. In real life Funicular is a Rails plugin, and the same components talk to a server: Funicular::HTTP.get fetches JSON, Funicular::Cable speaks ActionCable for live updates, a router maps paths like /chat/:channel_id to components, and the browser even gets a SQLite database of its own, kept in IndexedDB. At Sendai RubyKaigi 03 in November 2026, HASUMI Hitoshi built a chat with it in five minutes – without a line of JavaScript or a JavaScript build tool.
And one trick you have already seen at work: the same component code runs on CRuby too. Rails renders the first page on the server as HTML, and PicoRuby in the browser takes it over (server-side rendering). That is how your exercise was checked: CRuby rendered your traffic light without a browser and clicked its button three times.
rails new chat_demo
cd chat_demo
bundle add funicular
bin/rails funicular:install
bin/rails generate funicular:chat
bin/rails db:migrate
bin/rails funicular:compile
bin/rails serverThen open http://localhost:3000/funicular_chat in two windows and chat with yourself. The components live in app/funicular/; funicular:compile turns them into one app.mrb that PicoRuby.wasm loads. Without Rails, a <script> tag for PicoRuby.wasm and your component in <script type="text/ruby"> are enough, as in lesson 90.