ruby-on-rails - .rewind 方法对 ruby​​ 中的 Tempfile 做了什么?

标签 ruby-on-rails ruby stringio

我已经查看了 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/

相关文章:

ruby-on-rails - 在 Ruby 中的两个 bool 值之间切换

ruby-on-rails - 与 ActiveMerchant 一起使用的最佳支付网关是什么?

ruby-on-rails - 如何在 ruby​​ rxml 中转义分号

python PIL图像如何将图像保存到缓冲区以便以后使用?

ruby-on-rails - RubyMine - 关闭在 "empty"行中间单击的能力

ruby-on-rails - has_many 与多态关联中的 class_name

ruby - 将具有数组值的散列合并到一个数组中

javascript - jQuery/Javascript 替换 'Syntax error, unrecognized expression'

image - 如何使用 opencv python 库从内存缓冲区 (StringIO) 或 url 中读取图像

Python,在内存中写入zip到文件