What does the .rewind method in tempfile in ruby ​​do?

I looked through these documents and Google and it seems that I can not find the target .rewind , and how it differs from .close , in the context of working with Tempfile .

Also, why .read returns an empty string before rewinding?

Here is an example:

 file = Tempfile.new('foo') file.path # => A unique filename in the OS temp directory, # eg: "/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 
+5
source share
1 answer

Rewind Read more about it at ruby docs

IO # Close . Learn more about ruby docs

Read . Learn more about ruby docs

Summary

rewind
Positions ios at the beginning of input, resetting lineno to zero. Rewind resets 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 byte lengths from an input / output stream. The length must be a non-negative integer or zero. If the length is zero, an empty string ("") is returned.

+6
source

Source: https://habr.com/ru/post/1275061/


All Articles