Class: BigFiles::FileWithLines

Inherits:
Object
  • Object
show all
Defined in:
lib/bigfiles/file_with_lines.rb

Overview

Encapsulates a file which has a certain number of lines

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, file_opener: File) ⇒ FileWithLines

Returns a new instance of FileWithLines.



8
9
10
11
# File 'lib/bigfiles/file_with_lines.rb', line 8

def initialize(filename, file_opener: File)
  @filename = filename
  @file_opener = file_opener
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



6
7
8
# File 'lib/bigfiles/file_with_lines.rb', line 6

def filename
  @filename
end

Instance Method Details

#<=>(other) ⇒ Object



13
14
15
# File 'lib/bigfiles/file_with_lines.rb', line 13

def <=>(other)
  num_lines <=> other.num_lines
end

#num_linesObject



17
18
19
20
21
22
23
24
25
# File 'lib/bigfiles/file_with_lines.rb', line 17

def num_lines
  num_lines = 0
  @file_opener.open(filename, 'r') do |file|
    file.each_line do |_line|
      num_lines += 1
    end
  end
  num_lines
end