Class-level methods.
Methods
Public Instance methods
Module method to generate a random letter.
String::Random.rand_letter #=> "q" String::Random.rand_letter #=> "r" String::Random.rand_letter #=> "a"
[ show source ]
# File lib/more/facets/random.rb, line 352 def rand_letter (rand(26) + (rand(2) == 0 ? 65 : 97) ).chr 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/).
[ show source ]
# File lib/more/facets/random.rb, line 333 def random(max_length = 8, char_re = /[\w\d]/) # gmosx: this is a nice example of input parameter checking. # this is NOT a real time called method so we can add this # check. Congrats to the author. raise ArgumentError.new('char_re must be a regular expression!') unless char_re.is_a?(Regexp) string = "" while string.length < max_length ch = rand(255).chr string << ch if ch =~ char_re end return string end