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.



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

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

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/bigfiles/file_with_lines.rb', line 4

def filename
  @filename
end

Instance Method Details

#<=>(other) ⇒ Object



11
12
13
# File 'lib/bigfiles/file_with_lines.rb', line 11

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

#num_linesObject



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

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