Random::String::Self

Class-level methods.

Public Instance Methods

rand_letter() click to toggle source

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
random(max_length = 8, char_re = /[\w\d]/) click to toggle source

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.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.