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

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

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.

···

On Thu, Aug 19, 2010 at 5:32 PM, 3lionz Wexler <3lionzw@gmail.com> wrote:

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/\.