A complete answer to this needs to take into account the nature of the code inside the Ruby box.
Type 1: What I call "bare code" (no method definitions)There is just one or more lines of code which act upon the input values and send one or more output values. This behaves as follows...
- All of the code is executed when an input receives a trigger.
- All of the code is executed when the code is edited (literally every key-stroke!)
- The code may schedule any input to change value at a later time (when the code will again be executed).
- The code may schedule any output to change value at a later time (no code is executed).
These should NOT be executed at startup (the RubyEdit's output values are saved with the schematic, just like most other primitives). However, there seems to be a bug which does cause startup execution sometimes, which can lead to very annoying Ruby errors (
more info here).
Type 2: You write a series of method definitionsThese normally include a method called "event", which is used to handle incoming green triggers. These behave as follows...
- At startup, the code is parsed to define the methods, then the methods "init" and "loadState" are executed.
- When code is edited, the code is parsed to define the methods, then the "init" method is executed.
- When a green trigger arrives, the "event" method is called.
- The code may schedule any method to be executed at a later time.
- The code may schedule any input to change value at a later time (which will call "event", if present).
- The code may schedule any output to change value at a later time.
In at least some v3.0.x versions, the detection of the kind of code is very crude - it just looks for the string "def" anywhere in the code. This can occasionally lead to bugs where "bare code" is erroneously taken to be a set of method definitions - e.g. if you used the word "default" in a comment or to name a temporary local variable.