Module: PerfectWorld::Random

Defined in:
lib/perfect_world/random.rb

Overview

Random string generator.

Class Method Summary collapse

Class Method Details

.string(len = 64) ⇒ Object

Generates a random string with of a specified length (64 by default).

PerfectWorld::Random.string(32)
#=> "XzKk#~c\"Q(e2~8Bb#HO;v$}Jdid16-gO"

Returns a string.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/perfect_world/random.rb', line 15

def self.string(len = 64)
  s = String.new

  while s.length < len
    s << SecureRandom.random_bytes(len * 3.3).gsub(/[^[:graph:]]/, '')
  end

  s[0..(len - 1)].encode('utf-8')
rescue SystemCallError, NotImplementedError => e
  raise Error, e.message
end