简体   繁体   中英

Ruby not recognizing gets.chomp, I think?

I'm new to programming and started with Ruby through the Learn Ruby The Hard Way book. I'm on exercise 13 and am just fiddling around with utilizing ARGV and gets.chomp in the same script. 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 "

Any help would be appreciated.

That is what gets does . 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."

You could use $stdin.gets , or make sure ARGV is empty: ARGV.clear before using gets .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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