lib/garcon/core_ext/random.rb
# 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/).
#
# @example
#   String.random    # => 'dd4qed4r'
#
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/).
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 / ) .
def random(max_length = 8, char_re = /[\w\d]/)
        unless char_re.is_a?(Regexp)
          raise ArgumentError, 'second argument must be a regular expression'
        end
        string = ''
        while string.length < max_length
            ch = SecureRandom.random_number(255).chr
            string << ch if ch =~ char_re
        end
        return string
      end
def random ( max_length = 8 , char_re = / [ \w \d ] / ) unless char_re . is_a? ( Regexp ) raise ArgumentError , 'second argument must be a regular expression' end string = '' while string . length < max_length ch = SecureRandom . random_number ( 255 ) . chr string << ch if ch =~ char_re end return string end
Returns a randomly generated string.
Returns a randomly generated string .
max_length
<not_specific>
char_re
<not_specific>
example
String.random # => 'dd4qed4r'
String . random # = > ' dd4qed4r '