Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$filename = 'test.dat'

[ 'a', 'aa' ].each do |data|
  puts "=== Trying with data = '#{data}'"

  # Write data
  File.open($filename, 'w') { |io| io.write(data) }

  # Read data without calling io.eof?
  File.open($filename, 'r') do |io|
    read_data = io.read(1000)
    puts "Data read without calling io.eof? = #{read_data.inspect}"
  end

  # Read data with calling io.eof?
  File.open($filename, 'r') do |io|
    io.eof?
    read_data = io.read(1000)
    puts "Data read with calling io.eof?    = #{read_data.inspect}"
  end
end

# ===== ACTUAL OUTPUT
#
# === Trying with data = 'a'
# Data read without calling io.eof? = "a"
# Data read with calling io.eof?    = nil
# === Trying with data = 'aa'
# Data read without calling io.eof? = "aa"
# Data read with calling io.eof?    = "aa"
#
# ===== EXPECTED OUTPUT
#
# === Trying with data = 'a'
# Data read without calling io.eof? = "a"
# Data read with calling io.eof?    = "a"
# === Trying with data = 'aa'
# Data read without calling io.eof? = "aa"
# Data read with calling io.eof?    = "aa"