This is an implementation of a vending machine as per the spec from the Lambda Lounge language shootout (Vending Machine Specification)
You will need Ruby 1.9 to run the specs/code
The core concept of this implementation is illustrated as follows:
The VendingMachine owns all shared state managed by its two operating modes (VendingMode and ServiceMode). It uses a named context to execute the behaviors of its operating modes, delegating all interaction requests to whatever the active context state is.
Each named context uses Ruby's extend mechanism to add module methods to itself, then forwards some method calls it makes to the vending machine - the forwarded method calls allow for a clean separation between the shared-state owner (the VendingMachine) and the modes that need to operate on it (VendingMode and ServiceMode).
When the context object interacts with a forwarded method, the call looks the same as a local-method call. Ruby handles the method forwarding through the Forwardable module.
Copyright (c) 2009 Mario Aquino. See LICENSE for details.