Hello everyone I am a novice to learn Ruby
i am practicing to get the data from the file
puts "what is your filename?"
filename = $stdin.gets.chomp
Kernel#gets wraps around
IO#gets under the hood but before it needs to create an IO object, that IO object will be a concatenation of all command line arguments(ARGV) ( it will try to open every single one as file in positional order) and them read from it. If there are no elements left on ARGV it will then read from stdin.
That behavior can be tested with this simple snippet:
while gets puts $_ end
ruby myscript.rb without parameters it will read stdin, if parameters are specified it will try to open as files and read from it, it will works similarly to
cat command .
That stream could be tty, socket, file etc:
stream = File.open('/etc/passwd','r') puts stream.gets stream.close
A simple http client using sockets:
require 'socket' s = TCPSocket.new 'icanhazip.com',80 s.puts <<~EOF GET / HTTP/1.1 Host: icanhazip.com EOF while line = s.gets puts line end s.close