这个问题已经有了答案:
What's the difference between gets.chomp() vs. STDIN.gets.chomp()?
4答
filename = ARGV.first
txt = open(filename)
puts "Here's your file #{filename}:"
print txt.read
print "Type the filename again: "
file_again = $stdin.gets.chomp
这是我的问题,如果我把它改成
gets.chomp
它不起作用,为什么?txt_again = open(file_again)
print txt_again.read
gets.chomp
和$stdin.chomp
之间有什么区别? 最佳答案
根据Kernel#gets docs(重点矿井):
返回(并将argv(或$*)中文件列表的下一行指定给$\u),如果命令行中没有文件,则返回标准输入中的下一行。
在您的情况下,argv是非空的,因此Kernel#gets
适用于它:
关于ruby - Ruby gets.chomp和$ stdin.gets.chomp的区别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27401711/