Are they the same, or are there subtle differences between the two commands?
4 Answers
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
.
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.
http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-gets

- 123,080
- 26
- 284
- 201
-
Thanks for the explanation. Had a hard time looking through that difference in the documentations. – stanigator May 09 '12 at 20:29
-
1Doesn't it fall back to $stdin, which just happens to often be the same as STDIN? – echristopherson May 10 '12 at 06:49
gets.chomp()
= read ARGV
first
STDIN.gets.chomp()
= read user's input

- 6,709
- 13
- 37
- 58

- 321
- 3
- 2
If your color.rb file is
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
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

- 111
- 1
- 3
-
1Ruby white belt here currently chiseling through the granite walls of Ruby the Hard Way and this chapter had me stumped until I found your explanation. Thanks for this! – user2136000 Feb 17 '18 at 21:52
-
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.

- 163
- 1
- 4
- 9