Ruby Execution Order Experiment
Posted: Wed Nov 21, 2012 12:15 am
While working on some biggish Rubies, I've been scratching my head trying to prevent "no method for nil class" errors at startup. There are quite a few things that have to happen when a Ruby starts - loadState, fetching the stored input/output variables, the 'init' method etc.
So I devised a little test to see if I could work out once and for all what order these things happen...
Each section declares a variable, and also watches the values of all the other variables to work out which ones it is able to see - they'll return 'nil' if not declared yet. The results I get seem to indicate that the order is...
1) - Parse the code to make all the method definitions. Code not wrapped in a method will also get executed.
2) - The 'init' method will run if there is one
3) - loadState retrieves any variables stored at the last schematic save.
4) - The input and output variable arrays are read
5) - The bare code is parsed again! (Note how it seems to see all of the other variables - yet all of the other stages can see the "bare" variable. Two passes of the parser is the only reason I can think of for this)
6) - The rest of the schematic goes 'live', and events at the inputs start arriving.
Hopefully this knowledge will prevent so many load time errors due to undeclared variables being used too soon - e,g, don't refer to an input variable within the 'init' routine etc.
I'd be glad to hear from anyone who can spot any errors in my reasoning, though, as I'm still not 100% that I'm reading the results quite right!
PS) I'd also be glad of any explanation why I called the input variable 'doodad' instead of just plain old 'input'!!
So I devised a little test to see if I could work out once and for all what order these things happen...
Each section declares a variable, and also watches the values of all the other variables to work out which ones it is able to see - they'll return 'nil' if not declared yet. The results I get seem to indicate that the order is...
1) - Parse the code to make all the method definitions. Code not wrapped in a method will also get executed.
2) - The 'init' method will run if there is one
3) - loadState retrieves any variables stored at the last schematic save.
4) - The input and output variable arrays are read
5) - The bare code is parsed again! (Note how it seems to see all of the other variables - yet all of the other stages can see the "bare" variable. Two passes of the parser is the only reason I can think of for this)
6) - The rest of the schematic goes 'live', and events at the inputs start arriving.
Hopefully this knowledge will prevent so many load time errors due to undeclared variables being used too soon - e,g, don't refer to an input variable within the 'init' routine etc.
I'd be glad to hear from anyone who can spot any errors in my reasoning, though, as I'm still not 100% that I'm reading the results quite right!
PS) I'd also be glad of any explanation why I called the input variable 'doodad' instead of just plain old 'input'!!