IO#clone and 1.6 -> 1.8 question

D

dblack

Hi --

Just tinkering with IO#clone, in connection with some irc
chatting, and found the following behavior which puzzles
me. Here's the test program:

fh = File.new("nums") # 'nums' is 1..20, one integer per line
puts fh.gets

fd = fh.clone
puts fd.gets

puts fh.gets

and here's output from 1.6.8 and 1.8.0:

ruby 1.6.8 (2002-12-16) [i686-linux]
1
nil
2

ruby 1.8.0 (2003-08-04) [i686-linux]
1
2
nil

Actually I don't understand either of these; I thought that both runs
would produce 1\n2\n3\n.

Can anyone shed light on these examples?


David
 
Y

Yukihiro Matsumoto

Hi,

In message "IO#clone and 1.6 -> 1.8 question"

|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.

* 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".

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,125
Messages
2,570,748
Members
47,302
Latest member
MitziWragg

Latest Threads

Top