我已经查看了 these docs和谷歌,似乎无法找到 .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
最佳答案
倒带 - 在 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 ("").
关于ruby-on-rails - .rewind 方法对 ruby 中的 Tempfile 做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48530095/