Class-level methods.
Module method to generate a random letter.
String::Random.rand_letter #=> "q" String::Random.rand_letter #=> "r" String::Random.rand_letter #=> "a"
# File lib/more/facets/random.rb, line 389 389: def rand_letter 390: (Random.number(26) + (Random.number(2) == 0 ? 65 : 97) ).chr 391: end
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 370 370: def random(max_length = 8, char_re = /[\w\d]/) 371: # gmosx: this is a nice example of input parameter checking. 372: # this is NOT a real time called method so we can add this 373: # check. Congrats to the author. 374: raise ArgumentError.new('char_re must be a regular expression!') unless char_re.is_a?(Regexp) 375: string = "" 376: while string.length < max_length 377: ch = Random.number(255).chr 378: string << ch if ch =~ char_re 379: end 380: return string 381: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.