Permalink
Browse files

Minor refactoring

  • Loading branch information...
benburkert committed Nov 21, 2009
1 parent 702e6bb commit 8e979847ea01e350e5088caa94af48ae3e3dac7d
Showing with 6 additions and 6 deletions.
  1. +5 −3 lib/randexp/dictionary.rb
  2. +1 −3 lib/randexp/randgen.rb
@@ -10,15 +10,17 @@ def self.load_dictionary
end
def self.words(options = {})
- case
- when options.has_key?(:length)
+ if options.has_key?(:length)
words_by_length[options[:length]]
else
@@words ||= load_dictionary
end
end
def self.words_by_length
- @@words_by_length ||= words.inject({}) {|h, w| (h[w.size] ||= []) << w; h }
+ @@words_by_length ||= begin
+ hash = Hash.new {|h,k| h[k] = [] }
+ words.inject(hash) {|h, w| h[w.size] << w; h }
+ end
end
end
View
@@ -33,9 +33,7 @@ def self.alpha_numeric(options = {})
def self.word(options = {})
begin
word = Randexp::Dictionary.words(options).pick
- rescue
- word = ''
- options[:length].times { |iterator| word += alpha_numeric }
+ word ||= options[:length].of { alpha_numeric }.join
end until word =~ /^\w+$/
word

0 comments on commit 8e97984

Please sign in to comment.