Entry Point?

Does ruby have an entry point like Java & C have main?

For instance if i had the following code…

puts “Calling method”
method()

def method

do something here…

end

This will fail because the definition for method is after the call for
it
This wouldnt matter in Java or C with it being pre-compiled i guess.

So with this in mind i am wondering how best to organise my code.
Is there a default method i can override which will always be called
first?

You could use an END block. It is only executed once the entire script
has been read.

END{
?> say(“Farrel”)

}
=> nil

def say(something)
puts something
end
=> nil

exit
Farrel

Farrel

On Jun 22, 2006, at 10:07 AM, Azalar wrote:

This will fail because the definition for method is after the call for
it
This wouldnt matter in Java or C with it being pre-compiled i guess.

So with this in mind i am wondering how best to organise my code.
Is there a default method i can override which will always be called
first?

I usually have one script that kicks off a program based in an
external file of definitions. And that script looks like:

require ‘foo.rb’
Program::start()

That sort of thing. I usually use that first file to do things like
set up the environment, read parameters, etc… then pass them to
the rest of the program.

Check out how “irb” works. Basically that idea.
-Mat

Azalar wrote:

This will fail because the definition for method is after the call for
it
This wouldnt matter in Java or C with it being pre-compiled i guess.

So with this in mind i am wondering how best to organise my code.
Is there a default method i can override which will always be called
first?

Generally, you will see method and class definitions at the top of the
file, then the ‘main’ code down at the bottom. Or, even more commonly,
the classes are all in a separate file and 'require’d at the top of the
file which is executed.

Also, note that in languages like C++ you still have to have ‘forward’
class declarations or function prototypes in order to use them before
they are defined in the file. It isn’t much different.

However, in Ruby, there is no need for a ‘main’, although people use
different methods as mentioned in other replies.

-Justin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs