.rewind 方法对 ruby 中的临时文件有何作用?

What does the .rewind method do on a Tempfile in ruby?

我查看了 these docs 和 Google,似乎无法找到 .rewind 的用途以及它与 .close 的不同之处,在使用 Tempfile.

的上下文

另外,为什么 .read return 倒带前是一个空字符串?

这是一个例子:

file = Tempfile.new('foo')
file.path      # => A unique filename in the OS's temp directory,
               #    e.g.: "/tmp/foo.24722.0"
               #    This filename contains 'foo' in its basename.
file.write("hello world")
file.rewind
file.read      # => "hello world"
file.close
file.unlink    # deletes the temp file

Rewind - 在 ruby docs

上阅读更多相关信息

IO#Close - 阅读有关 ruby docs

的更多信息

阅读 - 阅读有关 ruby docs

的更多信息

总结

rewind
Positions ios to the beginning of input, resetting lineno to zero. Rewind resets the line number to zero

f = File.new("testfile")
f.readline   #=> "This is line one\n"
f.rewind     #=> 0
f.lineno     #=> 0
f.readline   #=> "This is line one\n"

IO#close
Closes ios and flushes any pending writes to the operating system.

read([length [, outbuf]])

Reads length bytes from the I/O stream. Length must be a non-negative integer or nil. If length is zero, it returns an empty string ("").