Class: Typer::LineCollection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_files) ⇒ LineCollection

Returns a new instance of LineCollection.



151
152
153
154
155
156
157
158
159
# File 'lib/typer.rb', line 151

def initialize(input_files)
  @input_files = input_files
  @lines = []
  Array(input_files).each do |input_file|
    @lines += File.read(input_file).split("\n")
  end
  @min_length = 100
  @max_length = 140
end

Instance Attribute Details

#input_filesObject

Returns the value of attribute input_files.



149
150
151
# File 'lib/typer.rb', line 149

def input_files
  @input_files
end

#max_lengthObject

Returns the value of attribute max_length.



149
150
151
# File 'lib/typer.rb', line 149

def max_length
  @max_length
end

#min_lengthObject

Returns the value of attribute min_length.



149
150
151
# File 'lib/typer.rb', line 149

def min_length
  @min_length
end

Instance Method Details

#get_randomObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/typer.rb', line 161

def get_random
  line = ''
  while line.length < @min_length do
    max_diff = @max_length - line.length
    item = nil
    iter = 0
    while item.nil? || item.length > max_diff - 1 do
      i = rand(@lines.size)
      item = @lines[i].strip
      iter += 1
      if iter > 10
        item = @lines[i].strip[0..max_diff - 2]
      end
    end
    line = line.length == 0 ? item : line + ' ' + item
  end
  line
end