pluginaweek / state_machine

Adds support for creating state machines for attributes on any Ruby class
http://www.pluginaweek.org
MIT License
3.74k stars 510 forks source link

ArgumentError with ruby-1.9.3 #220

Closed Gurpartap closed 11 years ago

Gurpartap commented 11 years ago

Things worked fine until I revisited my code a couple of months later using ruby-1.9.3 instead of the previous ruby-1.9.2. The class using state_machine can now not be initialized. This is the error I'm trying to troubleshoot.

/path/.bundle/ruby/1.9.1/gems/state_machine-1.1.2/lib/state_machine/machine.rb:1965:in `initialize': wrong number of arguments(2 for 0) (ArgumentError)
from /path/.bundle/ruby/1.9.1/gems/state_machine-1.1.2/lib/state_machine/machine.rb:1965:in `block in initialize'
from /path/.bundle/ruby/1.9.1/gems/state_machine-1.1.2/lib/state_machine/machine_collection.rb:34:in `initialize_states'
from /path/.bundle/ruby/1.9.1/gems/state_machine-1.1.2/lib/state_machine/machine.rb:1965:in `initialize' 

I have tried understanding the code attributed in backtrace. If super is omitted from machine.rb:1965's block, things seem to work (would probably break something elsewhere). So I'm looking for an informed opinion on whether this is a bug with state_machine, or if I am implementing something incorrectly (remember, it works fine on 1.9.2).

I'm using ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]. As tested, there was no such error with ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.2.0].

The project where state_machine is implemented, and the problem can be reproduced is here: https://github.com/Gurpartap/cognizant. Install the gem and run sudo cognizantd --trace /path/to/gems/cognizant/examples/cognizantd.yml to reproduce the error.

Thank you.

Gurpartap commented 11 years ago

As I read from StateMachine::MacroMethods documentation:

Because of the way the inclusion of modules works in Ruby, calling super() will not only call the superclass's initialize, but also initialize on all included modules. This allows the original state machine hook to get called properly.

Using initialize_state_machines instead of super in my model's initialize method solved the problem (since my model also had included modules).

:smile: