(How) Can you run another ruby script, from a ruby script?

Discussion in 'Ruby' started by 3lionz Wexler, Aug 19, 2010.

  1. Sorry for the newbie question, but Can you run another ruby script, from
    a ruby script?

    For example:
    #makes new .rb script
    f = File.open('test.rb', 'w+')
    f.write("puts 'Hello world'
    sleep 5 # seconds")
    f.close

    #run test.rb

    sleep 5 # seconds
    #wait 5 seconds
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    3lionz Wexler, Aug 19, 2010
    #1
    1. Advertising

  2. On Thu, Aug 19, 2010 at 5:32 PM, 3lionz Wexler <> wrote:
    > Sorry for the newbie question, but Can you run another ruby script, from
    > a ruby script?
    >
    > For example:
    > #makes new .rb script
    > f = File.open('test.rb', 'w+')
    > f.write("puts 'Hello world'
    > sleep 5 # seconds")
    > f.close


    It's better to use the block form of File.open, it ensures that the
    file is closed:

    File.open('test.rb', 'w+') do |f|
    f.write("puts 'Hello world'")
    sleep 5 # seconds
    end

    > #run test.rb


    load './test.rb'

    > sleep 5 # seconds
    > #wait 5 seconds
    > end


    Jesus.
     
    Jesús Gabriel y Galán, Aug 19, 2010
    #2
    1. Advertising

  3. 3lionz Wexler wrote:
    > #makes new .rb script
    > f = File.open('test.rb', 'w+')
    > f.write("puts 'Hello world'
    > sleep 5 # seconds")
    > f.close
    >
    > #run test.rb


    If the test.rb file that you are creating is a really just a temporary
    file and you don't need it after you create and execute it, then you
    could skip the file creation entirely by using eval() instead:

    my_new_script = "puts 'Hello world'
    sleep 5 # seconds"

    eval my_new_script # runs the Ruby script stored in the my_new_script
    string!

    However, eval() can be a double-edged sword, so be careful. Read more
    about it and you'll see what I mean.

    Cheers.
    --
    Posted via http://www.ruby-forum.com/.
     
    Suraj Kurapati, Aug 24, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. duncan
    Replies:
    2
    Views:
    6,144
  2. Replies:
    5
    Views:
    491
  3. souleymane yo
    Replies:
    1
    Views:
    173
    Thomas Jollans
    Jul 23, 2011
  4. Robert Cohen
    Replies:
    3
    Views:
    303
    Andrew Durstewitz
    Jul 15, 2003
  5. Jayson Williams
    Replies:
    28
    Views:
    367
    Mohit Sindhwani
    Nov 30, 2008
Loading...

Share This Page