Introducing Stateology, an implementation of the State Pattern using the Mixology C extension.
Use it like the following:
puts "Pets a puppy"
puts "Kicks a puppy"
s = Sample.new
# switch to Happy state
s.do_something #=> "Pets a puppy"
# now switch to Angry state
s.do_something #=> "Kicks a puppy"
The State Pattern is quite useful in game-dev where you may want different behaviours depending on whether the Player is Running, Jumping, Sleeping, etc
* Stateology is different to other implementations in that it works by dynamically mixing and unmixing modules from the target class.
* How to get Stateology: gem install stateology
* Note that stateology requires the mixology extension to work. The stateology gem will try to install the mixology gem itself and should work fine out of the box with ruby 1.8.6.
* However I am unsure whether the mixology maintainer applied my 1.9.1 patch to the mixology source - if you are trying to use Stateology with ruby 1.9.1 and encountering troubles
go here to get the 1.9.1 compatible version of mixology: http://github.com/banister/mixology19/tree/master