Introducing Stateology, an implementation of the State Pattern using the Mixology C extension.
Use it like the following:
class Sample
include Stateology
state(:Happy) {
def do_something
puts "Pets a puppy"
end
}
state(:Angry) {
def do_something
puts "Kicks a puppy"
end
}
end
s = Sample.new
# switch to Happy state
s.state :Happy
s.do_something #=> "Pets a puppy"
# now switch to Angry state
s.state :Angry
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