Class: RubyDice::Wordlist
- Inherits:
-
Object
- Object
- RubyDice::Wordlist
- Defined in:
- lib/ruby-dice/wordlist.rb
Instance Attribute Summary collapse
-
#words ⇒ Object
readonly
Returns the value of attribute words.
Instance Method Summary collapse
-
#initialize(filename_or_options = nil) ⇒ Wordlist
constructor
Damn ugly…
- #random(count = 5) ⇒ Object
Constructor Details
#initialize(filename_or_options = nil) ⇒ Wordlist
Damn ugly… lets refactor sometime
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ruby-dice/wordlist.rb', line 8 def initialize( = nil) = {} filename = nil if .is_a?(Hash) = filename = [:wordlist] else filename = end filename ||= 'diceware.wordlist.asc' filename += '.txt' if File.extname(filename) == '' search_paths = [ File.(File.join(File.dirname(__FILE__), '..', '..', 'assets')), File.join(Dir.home, '.wordlists') ] # Too convoluted? unless full_path = (File.exists?(filename) ? filename : nil) path = search_paths.detect { |path| File.exists? File.join(path, filename) } full_path = File.join(path, filename) end File.open(full_path, 'r:UTF-8') { |f| @words = f.read } @words = @words.match(/(11111.+66666.+?)\n/m)[1].split("\n").map { |l| l.split(/[\t ]/)[1].strip } end |
Instance Attribute Details
#words ⇒ Object (readonly)
Returns the value of attribute words.
5 6 7 |
# File 'lib/ruby-dice/wordlist.rb', line 5 def words @words end |
Instance Method Details
#random(count = 5) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/ruby-dice/wordlist.rb', line 37 def random(count = 5) (1..count).inject([]) do |selected| random = SecureRandom.random_number(@words.size) selected << @words[random] end end |