Forum: Ruby Run other programs

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7c8f1ac636d1e3ed78c4e3797312b8df?d=identicon&s=25 Jonathan Denni (bluebottle)
on 2007-01-25 19:06
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?
4371f8fa1c3574e74d8f88c77d23e7d7?d=identicon&s=25 Harry (Guest)
on 2007-01-30 03:51
(Received via mailing list)
On 1/26/07, Jonathan Denni <jonsdenni@gmail.com> 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"
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2007-01-30 04:26
(Received via mailing list)
From: Harry [mailto:ruby.hardware@gmail.com]:
# From Jonathan Denni <jonsdenni@gmail.com>:
# > 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
7c8f1ac636d1e3ed78c4e3797312b8df?d=identicon&s=25 Jonathan Denni (bluebottle)
on 2007-01-30 06:47
> 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/...
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')?
This topic is locked and can not be replied to.