Calling a Defined Method at bottom of script

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

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.

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.

Joel VanderWerf wrote:

foo

BEGIN {
def foo
puts “Foo!”
end
}

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

John

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
}

2008/10/3 Jack S. [email protected]:

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