(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

On Thu, Aug 19, 2010 at 5:32 PM, 3lionz Wexler [email protected]
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.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs