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

**URL:** <https://www.ruby-forum.com/t/how-can-you-run-another-ruby-script-from-a-ruby-script/193558>\
**Category:** Ruby\
**Created:** [August 19, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/how-can-you-run-another-ruby-script-from-a-ruby-script/193558 "2010-08-19T17:32:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![3lionz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/848f3c/32.png) [@3lionz](https://www.ruby-forum.com/u/3lionz)\
**Post date:** [August 19, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/how-can-you-run-another-ruby-script-from-a-ruby-script/193558/1 "2010-08-19T17:32:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![3lionz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/848f3c/32.png) [@3lionz](https://www.ruby-forum.com/u/3lionz)\
**Post date:** [August 19, 2010, 11:29pm UTC](https://www.ruby-forum.com/t/how-can-you-run-another-ruby-script-from-a-ruby-script/193558/2 "2010-08-19T23:29:33Z")

</div>

On Thu, Aug 19, 2010 at 5:32 PM, 3lionz Wexler [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![3lionz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/848f3c/32.png) [@3lionz](https://www.ruby-forum.com/u/3lionz)\
**Post date:** [August 24, 2010, 6:27am UTC](https://www.ruby-forum.com/t/how-can-you-run-another-ruby-script-from-a-ruby-script/193558/3 "2010-08-24T06:27:49Z")

</div>

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.
