# Newbie: def must come before call to def?

**URL:** <https://www.ruby-forum.com/t/newbie-def-must-come-before-call-to-def/88510>\
**Category:** Ruby\
**Created:** [March 12, 2007, 7:05am UTC](https://www.ruby-forum.com/t/newbie-def-must-come-before-call-to-def/88510 "2007-03-12T07:05:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![planetthoughtful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://www.ruby-forum.com/u/planetthoughtful)\
**Post date:** [March 12, 2007, 7:05am UTC](https://www.ruby-forum.com/t/newbie-def-must-come-before-call-to-def/88510/1 "2007-03-12T07:05:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![planetthoughtful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://www.ruby-forum.com/u/planetthoughtful)\
**Post date:** [March 12, 2007, 9:58am UTC](https://www.ruby-forum.com/t/newbie-def-must-come-before-call-to-def/88510/2 "2007-03-12T09:58:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![planetthoughtful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://www.ruby-forum.com/u/planetthoughtful)\
**Post date:** [March 12, 2007, 7:25am UTC](https://www.ruby-forum.com/t/newbie-def-must-come-before-call-to-def/88510/3 "2007-03-12T07:25:43Z")

</div>

On 3/12/07, planetthoughtful [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![planetthoughtful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://www.ruby-forum.com/u/planetthoughtful)\
**Post date:** [March 12, 2007, 10:13am UTC](https://www.ruby-forum.com/t/newbie-def-must-come-before-call-to-def/88510/4 "2007-03-12T10:13:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![planetthoughtful](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@planetthoughtful](https://www.ruby-forum.com/u/planetthoughtful)\
**Post date:** [March 12, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/newbie-def-must-come-before-call-to-def/88510/5 "2007-03-12T12:37:17Z")

</div>

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
