Class: NmaxReader
- Inherits:
-
Object
- Object
- NmaxReader
- Defined in:
- lib/nmax_reader.rb
Instance Attribute Summary collapse
-
#block_length ⇒ Object
readonly
Returns the value of attribute block_length.
Instance Method Summary collapse
- #capture_data(count_limit, verbose = false) ⇒ Object
-
#initialize ⇒ NmaxReader
constructor
A new instance of NmaxReader.
- #main ⇒ Object
Constructor Details
#initialize ⇒ NmaxReader
Returns a new instance of NmaxReader.
7 8 9 10 11 |
# File 'lib/nmax_reader.rb', line 7 def initialize @digit_buffer = '' @block_length = 121 @data_size = 0 end |
Instance Attribute Details
#block_length ⇒ Object (readonly)
Returns the value of attribute block_length.
5 6 7 |
# File 'lib/nmax_reader.rb', line 5 def block_length @block_length end |
Instance Method Details
#capture_data(count_limit, verbose = false) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/nmax_reader.rb', line 19 def capture_data(count_limit, verbose = false) maximum_numbers = NmaxCore.new(count_limit) loop do block = $stdin.read(@block_length) break if block.empty? @data_size += block.length print "\rReading: #{@data_size}." if verbose items = block.scan(/\d+/) # Extreme cases. # 1. If the entire read data block is a string if items.empty? && !@digit_buffer.empty? maximum_numbers.propose @digit_buffer.to_i @digit_buffer = '' end # 2. If the entire block of read data is a number, add it to the buffer if block =~ /^\d+$/ @digit_buffer << block next end # 3. If the first found number is the beginning of a block of read data - add it to the buffer and accept if block =~ /^\d/ && !items.empty? next if items.first.length == @block_length @digit_buffer << items.first maximum_numbers.propose @digit_buffer.to_i @digit_buffer = '' items.shift end # 4. If the last found number is the end of the block of read data - add it to the buffer if block =~ /\d$/ && !items.empty? @digit_buffer << items.last items.pop end # Common, the remaining numbers found, if any, are accepted without buffering items.each { |item| maximum_numbers.propose item.to_i } unless items.empty? break if block.length < @block_length end maximum_numbers.propose @digit_buffer.to_i if @digit_buffer.length.positive? puts "maximum_numbers.items: #{maximum_numbers.items.join(', ')}" maximum_numbers.items end |
#main ⇒ Object
13 14 15 16 17 |
# File 'lib/nmax_reader.rb', line 13 def main raise ArgumentError, 'You must pass argument - maximum length of matches found.' if ARGV.length != 1 capture_data(ARGV.first.to_i) end |