Created
May 13, 2012 01:20
-
-
Save mungruby/2670109 to your computer and use it in GitHub Desktop.
Modules in Modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module InitialState | |
def self.state | |
:unborn | |
end | |
end | |
module Baby | |
def self.state | |
:feed_me | |
end | |
end | |
module Child | |
def self.state | |
:teach_me | |
end | |
end | |
module Teen | |
def self.state | |
:pay_me | |
end | |
end | |
module Adult | |
def self.state | |
:marry_me | |
end | |
end | |
module FinalState | |
def self.state | |
:bury_me | |
end | |
end | |
module StateMachine | |
include InitialState | |
include Baby | |
include Child | |
include Teen | |
include Adult | |
include FinalState | |
def state | |
@current_state.state | |
end | |
def state_transition | |
@previous_state = @current_state | |
@current_state = @states.shift | |
end | |
def previous_state | |
@previous_state | |
end | |
end | |
class Life | |
include StateMachine | |
def initialize | |
@states = StateMachine.included_modules.reverse | |
@current_state = @states.shift | |
@previous_state = nil | |
end | |
def resurrect_me | |
if state == :bury_me | |
"sorry, death is the end" | |
end | |
end | |
end | |
c = Life.new | |
puts c.state | |
c.state_transition | |
puts c.state | |
# puts c.previous_state | |
c.state_transition | |
puts c.state | |
c.state_transition | |
puts c.state | |
c.state_transition | |
puts c.state | |
c.state_transition | |
puts c.state | |
puts c.resurrect_me | |
p c.class.ancestors | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment