[英]A ruby script to run other ruby scripts
If I want to run a bunch of ruby scripts (super similar, with maybe a number changed as a commandline argument) and still have them output to stdout, is there a way to do this? 如果我想运行一堆ruby脚本(超级类似,可能将数字作为命令行参数进行了更改)并且仍然将它们输出到stdout,有没有办法做到这一点?
ie a script to run these: 即运行这些脚本:
ruby program1.rb input_1.txt
ruby program1.rb input_2.txt
ruby program1.rb input_3.txt
like 喜欢
(1..3).each do |i|
ruby program1.rb input_#{i}'
end
in another script, so I can just run that script and see the output in a terminal from all 3 runs? 在另一个脚本中,因此我可以运行该脚本,并在终端中查看所有3次运行的输出?
EDIT: 编辑:
I'm struggling to implement the second highest voted suggested answer. 我正在努力实施投票数第二高的建议答案。
I don't have a main function within my program1.rb, whereas the suggested answer has one. 我的program1.rb中没有主要功能,而建议的答案有一个。
I've tried this, for script.rb: 我已经为script.rb尝试过这个:
require "program1.rb"
(1..6).each do |i|
driver("cmd_line_arg_#{i}","cmd_line_arg2")
end
but no luck. 但没有运气。 Is that right? 那正确吗?
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). 您可以使用load
运行所需的脚本( load
和require
之间的区别是require
如果已加载脚本,则不会再次运行该脚本)。
To make each run have different arguments (given that they are read from the ARGV variable), you need to override the ARGV variable: 为了使每个运行具有不同的参数(假设它们是从ARGV变量中读取的),您需要覆盖ARGV变量:
(1..6).each do |i|
ARGV = ["cmd_line_arg_#{i}","cmd_line_arg2"]
load 'program1.rb'
end
# script_runner.rb
require_relative 'program_1'
module ScriptRunner
class << self
def run
ARGV.each do | file |
SomeClass.new(file).process
end
end
end
end
ScriptRunner.run
. 。
# programe_1.rb
class SomeClass
attr_reader :file_path
def initialize(file_path)
@file_path = file_path
end
def process
puts File.open(file_path).read
end
end
Doing something like the code shown above would allow you to run: 像上面显示的代码一样,您可以运行:
ruby script_runner.rb input_1.txt input_2.txt input_3.txt
from the command line - useful if your input files change. 从命令行-如果输入文件更改,则很有用。 Or even: 甚至:
ruby script_runner.rb *.txt
if you want to run it on all text files. 如果要在所有文本文件上运行它。 Or: 要么:
ruby script_runner.rb inputs/*
if you want to run it on all files in a specific dir (in this case called 'inputs'). 如果要在特定目录中的所有文件上运行它(在这种情况下称为“输入”)。
This assumes whatever is in program_1.rb is not just a block of procedural code and instead provides some object (class) that encapsulates the logic you want to use on each file,meaning you can require program_1.rb once and then use the object it provides as many times as you like, otherwise you'll need to use #load: 这假定program_1.rb中的内容不仅是过程代码的块,而且提供了一些对象(类),该对象封装了要在每个文件上使用的逻辑,这意味着您可以一次使用program_1.rb,然后使用该对象提供任意多次,否则您将需要使用#load:
# script_runner.rb
module ScriptRunner
class << self
def run
ARGV.each do | file |
load('program_1.rb', file)
end
end
end
end
ScriptRunner.run
. 。
# program_1.rb
puts File.open(ARGV[0]).read
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.