Module | Random::String::Self |
In: |
lib/more/facets/random.rb
|
Class-level methods.
Returns a randomly generated string. One possible use is password initialization. Takes a max legnth of characters (default 8) and an optional valid char Regexp (default /\w\d/).
# File lib/more/facets/random.rb, line 336 336: def random(max_length = 8, char_re = /[\w\d]/) 337: # gmosx: this is a nice example of input parameter checking. 338: # this is NOT a real time called method so we can add this 339: # check. Congrats to the author. 340: raise ArgumentError.new('char_re must be a regular expression!') unless char_re.is_a?(Regexp) 341: string = "" 342: while string.length < max_length 343: ch = Random.number(255).chr 344: string << ch if ch =~ char_re 345: end 346: return string 347: end