On 3/12/07, planetthoughtful [email protected] wrote:
Very new to learning Ruby, so please forgive if this is a moronic
Not at all, this is a great question.
In Ruby, am I required to put my defs before any code that calls those
defs? I’m porting general skills across from PHP, where you can put a
function statement anywhere in a program, and the interpreter will
load the entire file before looking for the function definition.
Call me an optimist, but I think of this as Ruby gently trying to
encourage us to write better code. She’s friendly like that. Splaying
gobs of top level functions all over the place is the general pattern
PHP encourages, but in my humble opinion it’s not always the best
In Ruby, I would probably group similar functions together an put them
in a module, rather than defining them at the top level. Then you can
include that module later, when you need the functions.
Though of course, all the modules and their functions need to be
defined before you use them, but a common pattern in this case is to
split the modules into different files, and use ‘require’ to bring
them in at the top of the program where they are used. (therefore
ensuring definition before use)
That’s the long answer to your question.
The short answer is; ‘correct, you need to define the functions before
you call them in Ruby’