我正在做我的小新手项目。项目由两个脚本组成。脚本编号 1 是命令行界面,用于接收用户的参数。脚本号 2 创建笛卡尔积并将其写入文本文件。我的想法是在不将所有内容放入一个文件的情况下让一切正常工作)。当我尝试使用“加载”时,出现此错误:”
Carthese_product.rb:3:in `<top (required)>': undefined local variable or method
`x_min' for main:Object (NameError)
from D:/Cli_file.rb:25:in `load'
from D:/Cli_file.rb:25:in `<main>'
脚本 1(Cli_file.rb):
require 'trollop'
opts = Trollop::options do
banner <<-EOS
Welcome to points generator!
Usage:
test [options] <filenames>+
where [options] are:
EOS
opt :x_min, "Minimal value of X", :type => :int
opt :x_max, "Maximal value of X", :type => :int
opt :y_min, "Minimalna wartosc Y", :type => :int
opt :y_max, "Maksymalna wartosc Y", :type => :int
opt :interval, "Interval between points", :type => :int, :default => 1
opt :products_file, "Name of products_file", :type => :string, :default =>
"Products"
end
a= opts
x_min = a[:x_min]
x_max = a[:x_max]
y_min = a[:y_min]
y_max = a[:y_max]
interval = a[:interval]
products_file = a[:products_file]
load 'Carthese_product.rb'
Script2 (Carthese_product.rb)
products = []
(x_min/interval..x_max/interval).each do |x|
(y_min/interval..y_max/interval).each do|y|
products << [x*interval,y*interval]
end
end
a = products.map.with_index{|w, i| "#{i+1} #{w[0].to_s} #{w[1].to_s} \n"}
aFile = File.new( products_file, "w+")
if aFile
a.each{|x| aFile.write(x)}
else
puts "Something wrong!"
end
我知道最简单的解决方案是将所有内容放入一个脚本中,但出于我的教育目的,我想找到另一种方法!感谢您的帮助和兴趣!