Sha256: 6c1daf12905f266b72957dcd58cd27596964918a553c19cf5ee58b7601a05654

Contents?: true

Size: 853 Bytes

Versions: 3

Compression:

Stored size: 853 Bytes

Contents

#
# 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 goes to George Moschovitis.
#
# :NOTE: This is not very efficient. Better way?
#
#++
def String.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 = rand(255).chr
      string << ch if ch =~ char_re
  end

  return string
end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
# TODO

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/string/self/random.rb
facets-1.0.0 lib/facet/string/self/random.rb
facets-1.0.3 packages/core/lib/facet/string/self/random.rb