Traffic Light Simulation with Ruby

Posted on Sep 26, 2015

Today, I want to share an interesting project I found on GitHub: traffic_light. It’s a simulation of a traffic light controller, implemented in Ruby.

The project uses Ruby’s Fibers for concurrency and the Curses library for a terminal-based graphical interface. It’s a great example of how to use these technologies to create a simple simulation.

The core of the simulation is a state machine that defines the different states of the traffic lights (red, green, yellow). The state transitions are managed by the Fibers, which are lightweight concurrency primitives in Ruby.

Even though this project is written in Ruby, the concepts can be easily applied to other languages, including Elixir. In Elixir, you could use a GenServer to implement the state machine and a library like Phoenix LiveView to create a real-time web-based interface for the simulation.

It’s always inspiring to see how different languages and technologies can be used to solve similar problems. If you’re interested in state machines, concurrency, or just want to see a cool Ruby project, I highly recommend checking out the traffic_light repository.