简体   繁体   English

我觉得Ruby没有识别gets.chomp?

[英]Ruby not recognizing gets.chomp, I think?

I'm new to programming and started with Ruby through the Learn Ruby The Hard Way book. 我是编程新手,并通过Learn Ruby The Hard Way书开始使用Ruby。 I'm on exercise 13 and am just fiddling around with utilizing ARGV and gets.chomp in the same script. 我正在练习13,我只是在同一个脚本中使用ARGV和gets.chomp。 My code is as follows: 我的代码如下:

first, second, third = ARGV

puts "Your first variable is: #{first}"
puts "Your second variable is: #{second}"
puts "Your third is: #{third}"

print "Was that so difficult?"
answer = gets.chomp
print answer

Obviously this isn't that difficult of a script, but when I run it it I keep getting this error: "ex13.rb:in 'gets': No such file or directory @ rb_sysopen - 'it then prints the first argv the user inputs when starting the script' (Errno::ENOENT) from ex13.rb:8:in 'gets' from ex13.rb:8:in " 显然这不是一个脚本的困难,但当我运行它时,我不断收到这个错误:“ex13.rb:in'gets':没有这样的文件或目录@ rb_sysopen - '然后它打印第一个argv用户从ex13.rb启动脚本'(Errno :: ENOENT)时的输入:8:来自ex13.rb的'gets':8:in“

Any help would be appreciated. 任何帮助,将不胜感激。

That is what gets does . 就是gets Quoting: "( gets ) Returns (and assigns to $_) the next line from the list of files in ARGV (or $*), or from standard input if no files are present on the command line." 引用:“( gets )返回(并分配给$ _)ARGV(或$ *)中文件列表中的下一行,如果命令行中没有文件则返回标准输入。”

You could use $stdin.gets , or make sure ARGV is empty: ARGV.clear before using gets . 您可以使用$stdin.gets ,或确保ARGV为空: ARGV.clear在使用gets之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM