3
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

Hetal Khunti
  • 787
  • 1
  • 9
  • 23
Gent Binaku
  • 55
  • 11
  • Have you seen this [SO-Question1](http://stackoverflow.com/questions/12041492/ruby-whats-the-difference-between-stdin-gets-and-gets-chomp) and [SO-Question2](http://stackoverflow.com/questions/10523536/whats-the-difference-between-gets-chomp-vs-stdin-gets-chomp) – Hetal Khunti Dec 10 '14 at 13:16
  • 2
    http://stackoverflow.com/questions/10523536/whats-the-difference-between-gets-chomp-vs-stdin-gets-chomp has the answer with the precise difference. – Denis de Bernardy Dec 10 '14 at 13:55

1 Answers1

1

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:

Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154