Module Random::String::Self
In: lib/more/facets/random.rb

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"

[Source]

     # File lib/more/facets/random.rb, line 355
355:       def rand_letter
356:         (Random.number(26) + (Random.number(2) == 0 ? 65 : 97) ).chr
357:       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/).

[Source]

     # 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

[Validate]