Class: RubyLabs::RandomArray
- Inherits:
-
Array
- Object
- Array
- RubyLabs::RandomArray
- Defined in:
- lib/rubylabs.rb
Overview
RandomArray
Similar to TestArray, but draws random words from a file.
Constant Summary collapse
- @@sources =
{ :cars => "#{data}/cars.txt", :colors => "#{data}/colors.txt", :fruit => "#{data}/fruit.txt", :words => "#{data}/wordlist.txt", }
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(src, n) ⇒ RandomArray
constructor
A new instance of RandomArray.
Constructor Details
#initialize(src, n) ⇒ RandomArray
Returns a new instance of RandomArray.
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/rubylabs.rb', line 220 def initialize(src, n) fn = @@sources[src] or raise "RandomArray: undefined array type: #{src}" words = File.open(fn).readlines a = Hash.new while a.size < n w = words[rand(words.length)].chomp a[w] = 1 end a.keys.each do |w| self << w end end |
Class Method Details
.sources ⇒ Object
233 234 235 |
# File 'lib/rubylabs.rb', line 233 def RandomArray.sources return @@sources.keys.sort { |a,b| a.to_s <=> b.to_s } end |