Execute ruby file from a ruby file

Hi,
I can’t find out how can I execute my ruby script from another ruby
file.

This is how I would do it in command line
ruby jobs/eventParser.rb “5454353”

Greg

Greg Ma wrote:

I can’t find out how can I execute my ruby script from another ruby
file.

This is how I would do it in command line
ruby jobs/eventParser.rb “5454353”

system “ruby”, “jobs/eventParser.rb”, “5454353”

system “ruby jobs/eventParser.rb 5454353”

The latter is less secure since it passes the string to a shell for
splitting into words, and you have to worry about things like quoting,
but it lets you do redirection and shell pipelines, e.g.

system “ruby jobs/eventParser.rb 5454353 2>/dev/null”

However, it may be an idea to try to write your ruby code in such a way
as it could be used directly from the first ruby program. Then it might
become:

require ‘jobs/eventParser’
EventParser.new.run(“5454353”)

This is more efficient because you don’t spawn a whole new ruby
interpreter, and becomes even more efficient if you are repeatedly using
the EventParser object.

jobs/eventParser.rb can be written in such a way as it will work both
ways:

class EventParser
def run(args)

end
end
if FILE == $0
EventParser.new.run(ARGV)
end

HTH,

Brian.

On Tue, Jun 15, 2010 at 1:35 PM, Greg Ma [email protected] wrote:

Hi,
I can’t find out how can I execute my ruby script from another ruby
file.

This is how I would do it in command line
ruby jobs/eventParser.rb “5454353”

ri Kernel#system

---------------------------------------------------------- Kernel#system
system(cmd [, arg, …]) => true or false

 Executes _cmd_ in a subshell, returning +true+ if the command was
 found and ran successfully, +false+ otherwise. An error status is
 available in +$?+. The arguments are processed in the same way as
 for +Kernel::exec+.

    system("echo *")
    system("echo", "*")

 _produces:_

    config.h main.rb
    *

Kirk H.