Module: Random::StringExtensions::Self
- Defined in:
- lib/standard/facets/random.rb
Overview
Class-level methods.
Instance Method Summary collapse
-
#random(max_length = 8, char_re = /[\w\d]/) ⇒ Object
Returns a randomly generated string.
-
#random_binary(n_bytes) ⇒ Object
Generate a random binary string of
n_bytes
size.
Instance Method Details
#random(max_length = 8, char_re = /[\w\d]/) ⇒ Object
376 377 378 379 380 381 382 383 384 |
# File 'lib/standard/facets/random.rb', line 376 def random(max_length = 8, char_re = /[\w\d]/) raise ArgumentError.new('second argument 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 |