Ruby gets.chomp和$ stdin.gets.chomp的区别

时间:2014-12-10 13:01:36

标签: ruby learn-ruby-the-hard-way

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

之间的区别是什么?

1 个答案:

答案 0 :(得分:1)

根据Kernel#gets docs(强调我的):

  

从ARGV (或$ *)中的文件列表中返回(并分配给$ _)下一行,如果命令行中没有文件,则从标准输入返回。

在您的情况下,ARGV非空,因此Kernel#gets适用于它: