Class: CWG::CommonWords

Inherits:
Object
  • Object
show all
Defined in:
/Users/martyn/cw/cw_clone/lib/cw/common_words.rb

Constant Summary

CONFIG =
".cw_config"
HERE =
File.dirname(__FILE__) + '/'

Instance Method Summary (collapse)

Constructor Details

- (CommonWords) initialize

Returns a new instance of CommonWords



8
9
10
11
12
# File '/Users/martyn/cw/cw_clone/lib/cw/common_words.rb', line 8

def initialize
  @words = []
  path = File.join(HERE,'..','..','data','text')
  @file = File.join path, "english.txt"
end

Instance Method Details

- (Object) all



14
15
16
17
18
# File '/Users/martyn/cw/cw_clone/lib/cw/common_words.rb', line 14

def all
  File.foreach(@file).collect do |line|
  line.chomp
  end
end

- (Object) low(last)



20
21
22
23
24
25
26
27
28
29
30
31
# File '/Users/martyn/cw/cw_clone/lib/cw/common_words.rb', line 20

def low last
  results = []
  count = 0
  File.foreach(@file).collect do |line|
    if count <= last
      results << line.chomp
    end
    count += 1
    break if count > last
  end
  results
end

- (Object) mid(first, last)



33
34
35
36
37
38
39
40
41
42
43
44
# File '/Users/martyn/cw/cw_clone/lib/cw/common_words.rb', line 33

def mid first, last
  results = []
  count = 0
  File.foreach(@file).collect do |line|
    if (count >= first) && (count <= last)
      results << line.chomp
    end
    count += 1
    break if count > last
  end
  results
end

- (Object) parse_quantity(quantity = :default)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File '/Users/martyn/cw/cw_clone/lib/cw/common_words.rb', line 46

def parse_quantity(quantity = :default)
  if quantity == :default
    return [0, 999]
  elsif quantity.class == Fixnum
    [0, quantity - 1]
    (0...quantity).collect {|q| q}
  elsif quantity.class == Range
    ary = quantity.to_a
    return ary[0] - 1, ary[-1] -1
#        ary.pop
#        ary.unshift ary[0] - 1
#        ary
  end
end

- (Object) read(quantity = :default)



61
62
63
64
65
66
67
68
69
70
71
72
73
# File '/Users/martyn/cw/cw_clone/lib/cw/common_words.rb', line 61

def read(quantity = :default)
  if quantity == :all
    @words = all
  else
    qty = parse_quantity(quantity)
    if qty[0] == 0
      @words = low qty[-1]
    else
      @words = mid qty[0], qty[1]
    end
  end
  @words
end

- (Object) to_a



75
76
77
# File '/Users/martyn/cw/cw_clone/lib/cw/common_words.rb', line 75

def to_a
  @words
end