Class: Nifty::Utils::RandomString

Inherits:
Object
  • Object
show all
Defined in:
lib/nifty/utils/random_string.rb

Constant Summary collapse

LETTERS =
('A'..'Z').to_a + ('a'..'z').to_a
NUMBERS =
(0..9).to_a
WORD_CHARS =
LETTERS + NUMBERS
SYMBOLS =
['-', '!', '_', '@', '+', '*', '?', '%', '&', '/']

Class Method Summary collapse

Class Method Details

.generate(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/nifty/utils/random_string.rb', line 10

def self.generate(options = {})
  options[:length]  ||= 30
  options[:symbols] ||= false
  chars = WORD_CHARS
  chars += SYMBOLS if options[:symbols]
  (
    [LETTERS[rand(LETTERS.size)]] +
    (0...options[:length]-2).map{ chars[rand(chars.size)] } +
    [LETTERS[rand(LETTERS.size)]]
  ).join
end