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

标签 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 有什么区别

最佳答案

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

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.

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

关于Ruby gets.chomp 和 $stdin.gets.chomp 区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27401711/

相关文章:

ruby-on-rails - 为什么我应该只在测试环境而不是开发或生产环境中在我的 Rails 应用程序上运行 rspec 规范?

Ruby Exercise 18 undefined method 虽然我定义了方法

ruby - "Undefined method ' 关闭 '"试图关闭 Ruby 中的文件时

ruby - RSpec - 如何测试包含 HTTParty 的类

ruby - Readline.completion_append_character 不适用于 irb

ruby - Rails+ActiveAdmin - 使用 ransacker 过滤会抛出错误 PG::SyntaxError: ERROR: syntax error at or near ","

ruby-on-rails - 每当 : command not found

ruby - 正确的 Assert_Raise 单元测试和异常类的使用

ruby - 嵌套循环 : to be dynamic?

ruby - 学习 Ruby the Hardway ex。 42. 2 个文件中的 2 个类