class RandomStrings::Generator

Public Class Methods

new(&block) click to toggle source
# File lib/random_strings.rb, line 21
def initialize(&block)
  @config=Config.new(:tld_list, :default_string_length, :default_number_length)
  @config.tld_list=['.com', '.org', '.net']
  @config.default_string_length=10
  @config.default_number_length=5

  if block
    block.call @config
  end
end

Public Instance Methods

random_array_element(arr) click to toggle source
# File lib/random_strings.rb, line 48
def random_array_element(arr)
  arr[Random.rand*arr.size]
end
random_digits(count=-1) click to toggle source
# File lib/random_strings.rb, line 32
def random_digits(count=-1)
  if count == -1
    count = @config.default_number_length
  end
  selection = Range.new('0', '9').to_a
  count.times.map { selection[Random.rand()*selection.size] }.join ""
end
random_email() click to toggle source
# File lib/random_strings.rb, line 52
def random_email
  random_string + '@' + random_string + random_array_element(@config.tld_list)
end
random_string(count=-1) click to toggle source
# File lib/random_strings.rb, line 40
def random_string(count=-1)
  if count == -1
    count = @config.default_string_length
  end
  selection = (Range.new('a', 'z').to_a | Range.new('A', 'Z').to_a)
  count.times.map { selection[Random.rand()*selection.size] }.join ""
end
random_zip() click to toggle source
# File lib/random_strings.rb, line 56
def random_zip
  return random_digits 5
end