Class: FPM::Fry::BlockEnumerator

Inherits:
Struct
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fpm/fry/block_enumerator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_, blocksize = 128) ⇒ BlockEnumerator

Returns a new instance of BlockEnumerator.

Parameters:

  • io (IO)
  • blocksize (Numeric) (defaults to: 128)


7
8
9
# File 'lib/fpm/fry/block_enumerator.rb', line 7

def initialize(_, blocksize = 128)
  super
end

Instance Attribute Details

#blocksizeObject

Returns the value of attribute blocksize

Returns:

  • (Object)

    the current value of blocksize



2
3
4
# File 'lib/fpm/fry/block_enumerator.rb', line 2

def blocksize
  @blocksize
end

#ioObject

Returns the value of attribute io

Returns:

  • (Object)

    the current value of io



2
3
4
# File 'lib/fpm/fry/block_enumerator.rb', line 2

def io
  @io
end

Instance Method Details

#callString

Returns chunk or empty string at EOF.

Returns:

  • (String)

    chunk or empty string at EOF



24
25
26
27
28
29
30
# File 'lib/fpm/fry/block_enumerator.rb', line 24

def call
   while x = io.read(blocksize)
     next if x == ""
     return x
   end
   return ""
end

#each {|chunk| ... } ⇒ Enumerator

Returns unless called with a block.

Yields:

  • (chunk)

    One chunk from the io

Yield Parameters:

  • chunk (String)

Returns:

  • (Enumerator)

    unless called with a block



14
15
16
17
18
19
20
21
# File 'lib/fpm/fry/block_enumerator.rb', line 14

def each
  return to_enum unless block_given?
  # Reading bigger chunks is far more efficient than invoking #each on an IO.
  while chunk = io.read(blocksize)
    yield chunk
  end
  return nil
end