Module: Random::String::Self
- Defined in:
- lib/gems/facets-2.4.5/lib/more/facets/random.rb
Overview
Class-level methods.
Instance Method Summary collapse
-
#rand_letter ⇒ Object
Module method to generate a random letter.
-
#random(max_length = 8, char_re = /[\w\d]/) ⇒ Object
Returns a randomly generated string.
Instance Method Details
#rand_letter ⇒ Object
355 356 357 |
# File 'lib/gems/facets-2.4.5/lib/more/facets/random.rb', line 355 def rand_letter (Random.number(26) + (Random.number(2) == 0 ? 65 : 97) ).chr end |
#random(max_length = 8, char_re = /[\w\d]/) ⇒ Object
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/).
-- CREDIT George Moschovitis
NOTE This is not very efficient. Better way? ++
336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/gems/facets-2.4.5/lib/more/facets/random.rb', line 336 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 = Random.number(255).chr string << ch if ch =~ char_re end return string end |