Class: OmwRandomString

Inherits:
Object
  • Object
show all
Defined in:
lib/omw_random_string.rb

Class Method Summary collapse

Class Method Details

.generate(length = 16) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/omw_random_string.rb', line 2

def self.generate(length = 16)
  # allowed_chars contains the characters to be used by the generator
  allowed_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  
  # Contains the new random string
  random_string = ""
  
  # set seed for random number to time
  srand Time.now.to_i

  # lookup char given the random number as position of the char
  # add new char to random_string var
  # This is exectude 'length' times
  for i in 1..length do 
    random_string += allowed_chars[rand(allowed_chars.length)]
  end

  # return the random_string so it can be used in your code
  return random_string
end