# Run other programs

**URL:** <https://www.ruby-forum.com/t/run-other-programs/83392>\
**Category:** Ruby\
**Created:** [January 25, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/run-other-programs/83392 "2007-01-25T19:06:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [January 25, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/run-other-programs/83392/1 "2007-01-25T19:06:26Z")

</div>

lets say I have three programs

Main.rb  
Sub1.rb  
and  
Sub2.rb

When I run Main.rb, I want it to access and run the other two ruby  
programs. How do I do this?

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [January 30, 2007, 3:51am UTC](https://www.ruby-forum.com/t/run-other-programs/83392/2 "2007-01-30T03:51:39Z")

</div>

On 1/26/07, Jonathan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> lets say I have three programs
> 
> Main.rb  
> Sub1.rb  
> and  
> Sub2.rb
> 
> When I run Main.rb, I want it to access and run the other two ruby  
> programs. How do I do this?

If you just want to run them one at a time and wait, you can do this;

trya.rb  
puts “a”  
sleep 3  
system(“ruby tryb.rb”)  
system(“ruby tryc.rb”)  
sleep 3

tryb.rb  
puts “b”  
sleep 3

tryc.rb  
puts “c”

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [January 30, 2007, 4:26am UTC](https://www.ruby-forum.com/t/run-other-programs/83392/3 "2007-01-30T04:26:45Z")

</div>

From: Harry [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]:

# From Jonathan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

# \> lets say I have three programs

# \>

# \> Main.rb

# \> Sub1.rb

# \> and

# \> Sub2.rb

# \>

# \> When I run Main.rb, I want it to access and run the other two ruby

# \> programs. How do I do this?

# \>

# 

# If you just want to run them one at a time and wait, you can do this;

# 

# trya.rb

# puts “a”

# sleep 3

# system(“ruby tryb.rb”)

# system(“ruby tryc.rb”)

# sleep 3

# 

# tryb.rb

# puts “b”

# sleep 3

# 

# tryc.rb

# puts “c”

Just in case, he’s not familiar w ruby’s require yet.

C:\family\ruby\>cat main.rb  
puts **FILE**  
require ‘sub1.rb’  
require ‘sub2.rb’

C:\family\ruby\>cat sub1.rb  
puts **FILE**  
C:\family\ruby\>cat sub2.rb  
puts **FILE**  
C:\family\ruby\>ruby main.rb  
main.rb  
./sub1.rb  
./sub2.rb

C:\family\ruby\>qri require  
------------------------------------------------------ Multiple choice

```
 Kernel#require, Needle::Container#require,
 Needle::DefinitionContext#require

```

## C:\family\ruby\>qri kernel#require --------------------------------------------------------- Kernel#requi require(string) =\> true or false

```
 Ruby tries to load the library named _string_, returning +true+ i
 successful. If the filename does not resolve to an absolute path,
 it will be searched for in the directories listed in +$:+. If the
 file has the extension ``.rb'', it is loaded as a source file; if
 the extension is ``.so'', ``.o'', or ``.dll'', or whatever the
 default shared library extension is on the current platform, Ruby
 loads the shared library as a Ruby extension. Otherwise, Ruby tri
 adding ``.rb'', ``.so'', and so on to the name. The name of the
 loaded feature is added to the array in +$"+. A feature will not
 loaded if it's name already appears in +$"+. However, the file na
 is not converted to an absolute path, so that ``+require
 'a';require './a'+'' will load +a.rb+ twice.

```

hth  
kind regards -botp

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [January 30, 2007, 6:47am UTC](https://www.ruby-forum.com/t/run-other-programs/83392/4 "2007-01-30T06:47:47Z")

</div>

> If you just want to run them one at a time and wait, you can do this;
> 
> trya.rb  
> puts “a”  
> sleep 3  
> system(“ruby tryb.rb”)  
> system(“ruby tryc.rb”)  
> sleep 3
> 
> tryb.rb  
> puts “b”  
> sleep 3
> 
> tryc.rb  
> puts “c”

thank you!  
that was easy. so, according to  
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref\_m\_kernel.html#Kernel.system](http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html#Kernel.system)  
when I use system(" ") it has the same effect as if I typed whatever is  
in the quotes into the terminal, right?

I could also use load(“sub1.rb”) instead of system(“ruby sub1.rb”)  
correct?

Is there a way I can propogate the variables in the loaded files  
(‘sub1.rb’,sub2.rb’) to the loading file (‘main.rb’)?
