Ruby gets.chomp and $stdin.gets.chomp difference

2.3k Views Asked by At
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 

Here is my question if I change it to gets.chomp it doesn't work why ?

txt_again = open(file_again)

print txt_again.read

What's the difference between gets.chomp and $stdin.chomp

1

There are 1 best solutions below

0
On

Per the Kernel#gets docs (emphasis mine):

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.

In your case, ARGV is non-empty, so Kernel#gets applies to it: