Running Another Ruby Script from a Ruby Script

Running another ruby script from a ruby script

Avdi Grimm wrote a series of articles on the Devver blog about different ways to start Ruby subprocesses last summer:

  • A Dozen (or so) Ways to Start Subprocesses in Ruby: Part 1
  • A Dozen (or so) Ways to Start Subprocesses in Ruby: Part 2
  • A Dozen (or so) Ways to Start Subprocesses in Ruby: Part 3
  • Beware of pipe duplication in subprocesses

[Note: it appears that part 4 hasn't been published yet.]

Ruby - Running rb file from script

You may want to use open3

require 'open3'
cmd = 'ruby test1.rb'
#You may change the contents of cmd like you would run it from the command line; like ruby [directory]/filename
Open3.popen3(cmd) do |stdin, stdout|
var = stdout.read
puts var
end

A ruby script to run other ruby scripts

You can use load to run the script you need (the difference between load and require is that require will not run the script again if it has already been loaded).

To make each run have different arguments (given that they are read from the ARGV variable), you need to override the ARGV variable:

(1..6).each do |i|
ARGV = ["cmd_line_arg_#{i}","cmd_line_arg2"]
load 'program1.rb'
end

How to execute Ruby files from another Ruby file

In general your code isn't written in the Ruby way. This is untested but it looks about right:

class Launch

FILES = ['sum_of_digits', 'compressed_sequence', 'shortest_repetition']

def get_program
FILES.each_with_index do |fname, i|
puts "#{i} . #{fname}"
end

loop do
puts "Enter program number to execute: "
program_number = gets.to_i
file_to_load = FILES[program_number]
puts "loading program #{file_to_load}"

begin
system("ruby #{file_to_load}.rb #{file_to_load}.txt")
rescue => e
puts "loading error: #{e}"
puts "'#{file_to_load}' cannot be loaded, it may have been moved or not exist."
end

puts 'Do you want to continue Y/N'
break if gets.chomp.strip.upcase == 'N'
end
end
end

launch = Launch.new
launch.get_program

Some things to study:

  • block and end are used to start exception handling, not to define control loops. Well, they can, but there are better, more idiomatic, ways. loop is recommended by Matz.
  • You used load but I don't think that's really what you'd want to do. Instead, you should tell the OS to load and run the code in a sub-shell using system, not in the context of your currently running code.
  • Instead of using a bare rescue, your code should at least capture the exception using rescue => e so you can output what occurred. In "real life", AKA, production, you should be even more discerning and capture only the exceptions you expect, but that's a different discussion.
  • When using a begin/rescue/end, try to keep them as small as possible, at least until you're more familiar with how they work. rescue is a great way to shoot yourself in the foot, and debugging raised exceptions that could be generated by many lines of code can be a pain.

In general, when you have a list of things that's likely to change, or any variable that's more likely to change than the rest of the code, put that definition at the top of the script, or the top of the class or module definition, then reference it as a constant. That helps avoid magical dust being sprinkled through the code that has to be searched for if you want to add or delete things. Like files. Or magical dust.

Include another script in Ruby script

yes:

require_relative 'yourOtherFile'

You don't need to tack .rb on the end. The "relative" part in require_relative means "look in the same folder I am in for the other file"

If you want to access one variable from file A in file B then you have to make it a global ($x = 5). This will work but isn't a great way to organize your code (because it makes for an environment where the global scope is polluted with lots of different variables.

You should organize your code into classes:

class Planet
@@x = 0

def self.doStuffToX
@xx += 4
end

def self.getX
@@x
end
end

and you can require this file, then use Plant.getX and Plant.doStuffToX to access and change x.

Ruby - Run external file in new window

Pure Ruby is really just a scripting language without any GUI components or concepts of screens or windows.

However on Windows, and without using any GUI gem, you CAN create a new cmd.exe command prompt with a new script using a trick like this:

puts "I will run a separate script"
system('start cmd.exe /K ruby "some ruby script.rb"')
puts "I have finished."

This will run the Ruby script some ruby script.rb as a new process in a new command prompt window. Perhaps that gets you what you need.

How do I call a method that's in one Ruby script from another?

Modules are built for organizing methods and will be happy to be included. If you included a module, you no longer need the namespace.

require_relative "shared"
include CommonMethods
createLog # Call it simply


Related Topics



Leave a reply



Submit