Newbie: def must come before call to def?

Hi All,

Very new to learning Ruby, so please forgive if this is a moronic
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.

In Ruby, it appears that defs have to come first? So, the following is
right:

def beforecode
puts “appears before code that calls it”
end

beforecode

but this would be wrong?

aftercode

def aftercode
puts “appears after code that calls it”
end

Any help appreciated!

Regards,

pt

You should def anything before using it in any language, just good
habits. In C, for example you at least have to declare a function
prototype before using it, because then the compiler would know to
look for the prototype’s definition and plug it in.

On 3/12/07, planetthoughtful [email protected] wrote:

Hi All,

Very new to learning Ruby, so please forgive if this is a moronic
question.

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
idea.

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’

Regards,
-Harold

On Mon, Mar 12, 2007 at 03:05:09PM +0900, planetthoughtful wrote:

end

beforecode

but this would be wrong?

aftercode

def aftercode
puts “appears after code that calls it”
end

Yes. The reason is that unlike many other languages, Ruby does a lot
more
work at ‘run time’ - even defining methods and classes. This is one
reason
it’s such a dynamic language.

The first thing which Ruby does when reading in a source file is to
“parse”
it into a syntax tree - that is, it knows which things are expressions,
which things look like “def function … end” and so on. But it doesn’t
execute them. No classes are defined, no methods are defined.

During the parse phase, the only errors you’ll get are structural (e.g.
your
"end"s don’t balance)

Then it runs the code. If that code defines a method, then that method
only
gets defined when the definition is run.

So if you write:

aftercode
def aftercode

end

then when line 1 is executed, it finds that no method ‘aftercode’ exists
in
the current object, so it aborts.

HTH,

Brian.

planetthoughtful schrieb:

In Ruby, am I required to put my defs before any code that calls those
defs?

pt, in addition to what the others have said, note that you can do the
following:

def method_one
method_two
end

def method_two
puts “two”
end

method_one

The call to method_two is written before the definition of method_two,
but the call is executed after the definition has been parsed.

Regards,
Pit