在ruby和运行时args中传递命令行参数

时间:2015-01-28 03:55:27

标签: ruby

我在ruby中编写了一个代码,它基本上需要3个命令行args,然后是键盘上的userinput。问题是当我在没有命令行的情况下执行代码时,它能够读取userinput但是使用命令行抛出错误`gets':没有这样的文件或目录 - 3024(Errno :: ENOENT) 不明白为什么

class Matching
  attr_reader :sys_count
  def initialize()
    @sys_count = gets
    throw "Bad count" unless @sys_count.to_i > 0
  end
  def get_match
    count = @sys_count
    return nil unless count
    ret = count

    count.to_i.times do
      ret += gets
    end
    ((count.to_i * (count.to_i - 1))/2).times do
      while true do
        line = gets
        ret += line
        break if line == "\n"
      end
    end

    ret
  end

  def packet
    str = get_match
    return nil unless str
    "matched 0\njava\n" + str
  end
end
CONN=ARGV[0]
CONFIG=ARGV[1]
OUT_BASE=ARGV[2]
obj=Matching.new()
out=obj.packet
print out

是否有任何解决方法可以使其正常工作 〜

1 个答案:

答案 0 :(得分:1)

您需要明确声明您需要来自输入设备的输入,而不是命令行参数ARGV,并且由于gets是通用的,因此您必须具体说明避免混淆或误解的案例。

因此,请将所有gets替换为$stdin.getsSTDIN.gets,以获取标准输入的输入。

相关问题