# Calling a Defined Method at bottom of script

**URL:** <https://www.ruby-forum.com/t/calling-a-defined-method-at-bottom-of-script/150097>\
**Category:** Ruby\
**Created:** [October 3, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/calling-a-defined-method-at-bottom-of-script/150097 "2008-10-03T18:54:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![smithy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@smithy](https://www.ruby-forum.com/u/smithy)\
**Post date:** [October 3, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/calling-a-defined-method-at-bottom-of-script/150097/1 "2008-10-03T18:54:18Z")

</div>

It seems that I have to place all the methods I have created at the top  
of my script in order to be able to call them later on in the script.

Is there a way for me to put all my methods at the bottom of the script  
and still be able to call them at the top of the script?

Thanks

John

---

<div class="post-metadata">

**Author:** ![smithy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@smithy](https://www.ruby-forum.com/u/smithy)\
**Post date:** [October 3, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/calling-a-defined-method-at-bottom-of-script/150097/2 "2008-10-03T19:00:15Z")

</div>

Jack S. wrote:

> It seems that I have to place all the methods I have created at the top  
> of my script in order to be able to call them later on in the script.
> 
> Is there a way for me to put all my methods at the bottom of the script  
> and still be able to call them at the top of the script?
> 
> Thanks
> 
> John

Hello.

As Ruby in an ObjectOriented programming language, it’s best not to  
create unbound methods, but to encapsulate them as class methods, and  
then you can just instantiate the class, and do all the work in the  
initialize method. Order of method declarations in class is not  
important (unless you use meta-programming tricks), so you can have your  
initializer at the top of the class.

If you don’t want to do it like this, you can just write your main code  
in a separate method at the beginning of your script, and just call the  
method (one line) at the very bottom.

TPR.

---

<div class="post-metadata">

**Author:** ![smithy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@smithy](https://www.ruby-forum.com/u/smithy)\
**Post date:** [October 3, 2008, 7:38pm UTC](https://www.ruby-forum.com/t/calling-a-defined-method-at-bottom-of-script/150097/3 "2008-10-03T19:38:31Z")

</div>

Jack S. wrote:

> Is there a way for me to put all my methods at the bottom of the script  
> and still be able to call them at the top of the script?

In short: no. You can only call methods after they are defined.

---

<div class="post-metadata">

**Author:** ![smithy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@smithy](https://www.ruby-forum.com/u/smithy)\
**Post date:** [October 3, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/calling-a-defined-method-at-bottom-of-script/150097/4 "2008-10-03T20:24:15Z")

</div>

Joel VanderWerf wrote:

> foo
> 
> BEGIN {  
> def foo  
> puts “Foo!”  
> end  
> }

I used this and it worked nicely…thanks for the help!

John

---

<div class="post-metadata">

**Author:** ![smithy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@smithy](https://www.ruby-forum.com/u/smithy)\
**Post date:** [October 3, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/calling-a-defined-method-at-bottom-of-script/150097/5 "2008-10-03T19:57:55Z")

</div>

Jack S. wrote:

> It seems that I have to place all the methods I have created at the top  
> of my script in order to be able to call them later on in the script.
> 
> Is there a way for me to put all my methods at the bottom of the script  
> and still be able to call them at the top of the script?

Sure,

====== one way =====  
END {  
foo  
}

def foo  
puts “Foo!”  
end

====== another way =====  
foo

BEGIN {  
def foo  
puts “Foo!”  
end  
}

---

<div class="post-metadata">

**Author:** ![smithy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@smithy](https://www.ruby-forum.com/u/smithy)\
**Post date:** [October 6, 2008, 1:19pm UTC](https://www.ruby-forum.com/t/calling-a-defined-method-at-bottom-of-script/150097/6 "2008-10-06T13:19:44Z")

</div>

2008/10/3 Jack S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Joel VanderWerf wrote:
> 
> > foo
> > 
> > BEGIN {  
> > def foo  
> > puts “Foo!”  
> > end  
> > }
> 
> I used this and it worked nicely…thanks for the help!

Yet another way:

def main(argv)  
foo  
end

def foo  
end

def other\_methods  
end

main(ARGV)

Cheers

robert
