#!/usr/bin/env ruby
# ex:ts=8
# vim:sts=4
# vim:sw=4
#
# Implement the State pattern
#
#
class State
    def change_state(new_method)
	raise ArgumentError, "Unknown method #{new_method.inspect}", caller \
	    unless respond_to? new_method
	c = (class <<self; self; end)
	c.class_eval { alias_method :foo, new_method }
    end
    def say_foo
	puts "foo"
    end
    def say_bar
	puts "bar"
    end
end

if __FILE__ == $0
    state = State.new
    state.change_state(:say_foo)
    state.foo
    state.change_state(:say_bar)
    state.foo
end
