In response to toro2k's comment, I'm taking the freedom of showing how I do things at home in cases like this one:
module Ext1
def f
puts "state a action"
end
end
module Ext2
def f
puts "state b action"
end
end
class X
attr_reader :state
def initialize( state=:a )
@state = state
extend case state
when :a then Ext1
when :b then Ext2
end
end
end
a, b = [ :a, :b ].map &X.method( :new )
a.f # ...
b.f # ...
But this is quite different from the line of thinking that Pavel has asked about in his question.
Note: Pavel's instance "state" never changes, so there are no transitions. Should there be multiple states and transitions between them, you would have a place/transition net, aka. Petri net sensu lato, embedded in the instance. This would be handled by my y_petri
gem, in which places and transitions are objects on their own right and belong to a Net
object, which would be an attribute of the MyClass
instance, and whose state vector would govern its behavior in the way similar to the one that you or Pavel suggested. Another gem that can be considered is the state machine
gem.