root/lib/randomizer.rb

Download in other formats: Raw | Text
Revisions
Dimitrij Denissenko
Dimitrij Denissenko
Jan 24 2009 * 11:51
(over 1 year ago)

Revision b1794537d634e7833190d162a0351bc60046c8fc

Initial public import

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Randomizer
  extend self
  
  def string(size = 8)
    size -= 1
    [Array.new(size){rand(256).chr}.join].pack('m')[0..size]  
  end

  def pronounceable(size = 12)
    chars = [ char(:c) + char(:v) ]
    (size / 2).to_i.times do 
      chars << cchars + char(:v)
    end
    chars.join.first(size)
  end
    
  private
  
    def char(type)
      set = (type == :v) ? ['a','e','i','o','u'] : ['b','c','d','f','g','k','l','m','n','p','r','s','t','w']
      set[rand(set.size)].send(rand(3).zero? ? :upcase : :downcase)
    end
    
    def cchars
      rand(3).zero? ? char(:c) : char(:c) + char(:c)
    end

end