.rewind方法对ruby中的Tempfile有什么作用?

Jon*_*orn 12 ruby ruby-on-rails stringio

我已经通过看这些文档和谷歌,似乎无法找到的目的.rewind,以及它如何从不同.close,在工作的环境Tempfile.

另外,为什么.read在倒带之前返回一个空字符串?

这是一个例子:

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
Run Code Online (Sandbox Code Playgroud)

iam*_*ric 14

倒带 - 在ruby docs上阅读更多相关信息

IO#Close - 阅读有关ruby文档的更多信息

阅读 - 阅读有关ruby文档的更多信息

摘要

rewind
将ios定位到输入的开头,将lineno重置为零.倒带将行号重置为零

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

IO#close
关闭ios并刷新对操作系统的任何挂起写入.

([length [,outbuf]])

从I/O流中读取长度字节.长度必须是非负整数或零.如果length为零,则返回空字符串("").