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

···

--
Posted via http://www.ruby-forum.com/.

ri Kernel#system

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

···

On Tue, Jun 15, 2010 at 1:35 PM, Greg Ma <gregorylepacha@msn.com> 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"

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

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.

···

--
Posted via http://www.ruby-forum.com/\.