Execute ruby file from a ruby file

G

Greg Ma

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
 
K

Kirk Haines

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 Haines
 
B

Brian Candler

Greg said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top