From: Dave Thomas Date: 2002-04-05T07:09:27+09:00 Subject: Re: Kernet methods and IO methods Yohanes Santoso writes: > Markus Jais writes: > > > that is, the IO#gets is completely different from Kernel's "gets" method. > > I do not mean, that they work differently, but they are implemented > > seperately in Kernel and again in IO. > > Think of Kernel#gets as: > def Kernel.gets > STDIN.gets > end Actually, it's slightly more complicated than that. If there are command line file names present, gets will read from each in turn. This doesn't affect STDIN or $stdin. Thus while line = gets puts line end will read from files specified on the command line (if any) while STDIN.gets will read from standard input regardless. If there are no command line file names present, Ruby initially sets $stdin to STDIN (the process's standard input), and then reads using $stdin as a file. In these circumstances, you can set the value of $stdin yourself. If that object is an IO object, it will be read from. It it's another class, its #gets method will be called. class Dave def initialize @values = %w{d a v e} end def gets @values.shift end end $stdin = Dave.new while line = gets puts line end However, I'm beginning to wonder if it's possibly a bug that the above program acts differently depending on whether it's called with command line arguments. Cheers Dave