Calling a Defined Method at bottom of script


#1

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


#2

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.


#3

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.


#4

Joel VanderWerf wrote:

foo

BEGIN {
def foo
puts “Foo!”
end
}

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

John


#5

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
}


#6

2008/10/3 Jack S. 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