【问题标题】:passing command line args in ruby and runtime args through gets通过gets传递ruby中的命令行参数和运行时参数
【发布时间】:2015-01-28 03:55:27
【问题描述】:

我用 ruby​​ 编写了一个代码,它基本上需要 3 个命令行参数,然后是键盘的用户输入。问题是当我在没有命令行的情况下执行代码时,它能够读取用户输入,但使用命令行却抛出错误“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

是否有任何解决方法可以使它工作 ~

【问题讨论】:

    标签: ruby


    【解决方案1】:

    您需要明确说明您需要来自输入设备的输入,而不是命令行参数ARGV,并且由于gets 是通用的,因此在这种情况下您必须具体说明以避免混淆或误解.

    因此,将所有gets 替换为$stdin.getsSTDIN.gets,后者从标准输入中获取输入。

    【讨论】:

    • @Sur123 很高兴能帮上忙 :)
    猜你喜欢
    • 2016-03-25
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    相关资源
    最近更新 更多