Module: Safeguard::CoreExt::File

Defined in:
lib/safeguard/core_ext/file.rb

Overview

Ruby core extensions for the File class.

Constant Summary collapse

DEFAULT_CHUNK_SIZE =

Default size of chunks in bytes. A megabyte is equal to 2^20 bytes, or 1,048,576 bytes.

2 ** 20

Instance Method Summary collapse

Instance Method Details

#each_chunk(chunk_size = DEFAULT_CHUNK_SIZE) ⇒ Object

Reads a file piece by piece, yielding every chunk until the end of the file. Will yield chunks of DEFAULT_CHUNK_SIZE bytes, unless specified.



13
14
15
16
17
18
19
# File 'lib/safeguard/core_ext/file.rb', line 13

def each_chunk(chunk_size = DEFAULT_CHUNK_SIZE)
  # A chunk_size of zero will cause an infinite loop. It will keep reading
  # 0 bytes, so the pointer will never change and the end of the file will
  # never be reached.
  bytes = case chunk_size when nil, 0 then nil else chunk_size.abs end
  yield read bytes until eof?
end