Are they the same, or are there subtle differences between the two commands?
它们是相同的,还是两个命令之间存在细微差别?
4 个解决方案
#1
36
gets
will use Kernel#gets
, which first tries to read the contents of files passed in through ARGV
. If there are no files in ARGV
, it will use standard input instead (at which point it's the same as STDIN.gets
.
gets将使用Kernel#gets,它首先尝试读取通过ARGV传入的文件的内容。如果ARGV中没有文件,它将使用标准输入(此时它与STDIN.gets相同)。
Note: As echristopherson pointed out, Kernel#gets
will actually fall back to $stdin
, not STDIN
. However, unless you assign $stdin
to a different input stream, it will be identical to STDIN
by default.
注意:正如echristopherson所指出的,Kernel#gets实际上会回落到$ stdin,而不是STDIN。但是,除非将$ stdin分配给不同的输入流,否则默认情况下它与STDIN相同。
http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-gets
http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-gets
#2
20
gets.chomp() = read ARGV first
gets.chomp()=首先读取ARGV
STDIN.gets.chomp() = read user's input
STDIN.gets.chomp()=读取用户的输入
#3
4
If your color.rb file is
如果你的color.rb文件是
first, second, third = ARGV
puts "Your first fav color is: #{first}"
puts "Your second fav color is: #{second}"
puts "Your third fav color is: #{third}"
puts "what is your least fav color?"
least_fav_color = gets.chomp
puts "ok, i get it, you don't like #{least_fav_color} ?"
and you run in the terminal
你在终端跑
$ ruby color.rb blue yellow green
it will throw an error (no such file error)
它会抛出一个错误(没有这样的文件错误)
now replace 'gets.chomp' by 'stdin.gets.chomp' on the line below
现在用'stdin.gets.chomp'替换'gets.chomp',如下所示
least_fav_color = $stdin.gets.chomp
and run in the terminal the following command
并在终端中运行以下命令
$ ruby color.rb blue yellow green
then your program runs!!
然后你的程序运行!!
Basically once you've started calling ARGV from the get go (as ARGV is designed to) gets.chomp can't do its job properly anymore. Time to bring in the big artillery: $stdin.gets.chomp
基本上,一旦你开始调用ARGV(如ARGV的设计),gets.chomp就不能再正常工作了。是时候引进大炮了:$ stdin.gets.chomp
#4
3
because if there is stuff in ARGV, the default gets method tries to treat the first one as a file and read from that. To read from the user's input (i.e., stdin) in such a situation, you have to use it STDIN.gets explicitly.
因为如果ARGV中有东西,默认的获取方法会尝试将第一个作为文件处理并从中读取。要在这种情况下读取用户的输入(即stdin),必须明确地使用STDIN.gets。
#1
36
gets
will use Kernel#gets
, which first tries to read the contents of files passed in through ARGV
. If there are no files in ARGV
, it will use standard input instead (at which point it's the same as STDIN.gets
.
gets将使用Kernel#gets,它首先尝试读取通过ARGV传入的文件的内容。如果ARGV中没有文件,它将使用标准输入(此时它与STDIN.gets相同)。
Note: As echristopherson pointed out, Kernel#gets
will actually fall back to $stdin
, not STDIN
. However, unless you assign $stdin
to a different input stream, it will be identical to STDIN
by default.
注意:正如echristopherson所指出的,Kernel#gets实际上会回落到$ stdin,而不是STDIN。但是,除非将$ stdin分配给不同的输入流,否则默认情况下它与STDIN相同。
http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-gets
http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-gets
#2
20
gets.chomp() = read ARGV first
gets.chomp()=首先读取ARGV
STDIN.gets.chomp() = read user's input
STDIN.gets.chomp()=读取用户的输入
#3
4
If your color.rb file is
如果你的color.rb文件是
first, second, third = ARGV
puts "Your first fav color is: #{first}"
puts "Your second fav color is: #{second}"
puts "Your third fav color is: #{third}"
puts "what is your least fav color?"
least_fav_color = gets.chomp
puts "ok, i get it, you don't like #{least_fav_color} ?"
and you run in the terminal
你在终端跑
$ ruby color.rb blue yellow green
it will throw an error (no such file error)
它会抛出一个错误(没有这样的文件错误)
now replace 'gets.chomp' by 'stdin.gets.chomp' on the line below
现在用'stdin.gets.chomp'替换'gets.chomp',如下所示
least_fav_color = $stdin.gets.chomp
and run in the terminal the following command
并在终端中运行以下命令
$ ruby color.rb blue yellow green
then your program runs!!
然后你的程序运行!!
Basically once you've started calling ARGV from the get go (as ARGV is designed to) gets.chomp can't do its job properly anymore. Time to bring in the big artillery: $stdin.gets.chomp
基本上,一旦你开始调用ARGV(如ARGV的设计),gets.chomp就不能再正常工作了。是时候引进大炮了:$ stdin.gets.chomp
#4
3
because if there is stuff in ARGV, the default gets method tries to treat the first one as a file and read from that. To read from the user's input (i.e., stdin) in such a situation, you have to use it STDIN.gets explicitly.
因为如果ARGV中有东西,默认的获取方法会尝试将第一个作为文件处理并从中读取。要在这种情况下读取用户的输入(即stdin),必须明确地使用STDIN.gets。