[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Calling a Defined Method at bottom of script

Jack Smith

10/3/2008 4:53:00 PM

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
--
Posted via http://www.ruby-....

5 Answers

Sebastian Hungerecker

10/3/2008 4:58:00 PM

0

Jack Smith 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.

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

TPReal

10/3/2008 4:59:00 PM

0

Jack Smith 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.
--
Posted via http://www.ruby-....

Joel VanderWerf

10/3/2008 5:55:00 PM

0

Jack Smith 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
}


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Jack Smith

10/3/2008 6:23:00 PM

0

Joel VanderWerf wrote:
> foo
>
> BEGIN {
> def foo
> puts "Foo!"
> end
> }

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

John
--
Posted via http://www.ruby-....

Robert Klemme

10/6/2008 11:17:00 AM

0

2008/10/3 Jack Smith <js@priceline.com>:
> 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



--
remember.guy do |as, often| as.you_can - without end