lib/rantly/generator.rb in rantly-0.3.0 vs lib/rantly/generator.rb in rantly-0.3.1

- old
+ new

@@ -99,11 +99,15 @@ def classify(classifier) @classifiers[classifier] += 1 end def guard(test) - raise GuardFailure.new unless test + unless test + raise GuardFailure.new + else + true + end end def size @size || Rantly.default_size end @@ -203,24 +207,31 @@ end end end def array(n=self.size,&block) - acc = [] - n.times { acc << self.instance_eval(&block) } - acc + n.times.map { self.instance_eval(&block) } end + def dict(n=self.size,&block) + h = {} + each(n) do + k,v = instance_eval(&block) + h[k] = v if guard(!h.has_key?(k)) + end + h + end + module Chars class << self ASCII = "" (0..127).to_a.each do |i| ASCII << i end def of(regexp) - ASCII.scan(regexp).to_a.map! { |char| char[0] } + ASCII.scan(regexp).to_a.map! { |char| char[0].ord } end end ALNUM = Chars.of /[[:alnum:]]/ ALPHA = Chars.of /[[:alpha:]]/