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

3

3lionz Wexler

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
 
J

Jesús Gabriel y Galán

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

Suraj Kurapati

3lionz said:
#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.
 

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

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top