Class: Wc::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/wc/reader.rb

Constant Summary collapse

HIDE_LIST =
['or', 'the', 'of', 'a', 'if', 'to', 'and', 'in', 'is', 'are']

Class Method Summary collapse

Class Method Details

.consume(a_text) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/wc/reader.rb', line 16

def self.consume(a_text)
  occurrences = Hash.new { |h, k| h[k] = 0 }

  words = a_text.split
  words.each { |w|
    if ! HIDE_LIST.include?(w.downcase)
      occurrences[w.downcase] += 1 
    end
  }
  occurrences
end

.read(filename, hide_list = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/wc/reader.rb', line 5

def self.read(filename, hide_list=nil) 
  occurrences = Hash.new { |h, k| h[k] = 0 }

  File.open(filename, "r") { |f|
    f.each_line { |line|
      Wc::Reader.consume(line)
    }
  }
  occurrences
end