Hi --

On Tue, 9 Sep 2003, Yukihiro Matsumoto wrote:

> Hi,
>
> In message "IO#clone and 1.6 -> 1.8 question"
>     on 03/09/09, dblack / superlink.net <dblack / superlink.net> writes:
>
> |Just tinkering with IO#clone, in connection with some irc
> |chatting, and found the following behavior which puzzles
> |me.  Here's the test program:
>
> It's combination of the two:
>
>   * file position is shared among IO clones.  It's defined stdio
>     behavior.

That's what I was expecting, but:

  fh = File.new("nums")
  fd = fh.clone
  fh.gets
  puts fh.pos
  puts fd.pos

  =>
     2
     0

>   * IO are buffered so that actual file pointer
>     may advance more than you expected (i.e. entire "num" file was
>     swallowed this case) by a single "gets".

I'm really puzzled by that.  If that's the case, how is it possible to
iterate through the lines of a file?  Doesn't it have to be guaranteed
that gets will return the next line?

Also, the thing I'm seeing only seems to happen when a file handle is
cloned.  If I just do:

  fh = File.new("nums")
  20.times { puts fh.gets }

I always get 20 lines of output.  But when I clone fh, I start to get
the behavior where one fh advances to EOF and the other doesn't.  I'm
afraid I'm still not seeing why cloning a file handle would or should
cause any of this to happen.


David

-- 
David Alan Black
home: dblack / superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav