Class: RandomStrings::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/random_strings.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Generator

Returns a new instance of Generator.



21
22
23
24
25
26
27
28
29
30
# 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

Instance Method Details

#random_array_element(arr) ⇒ Object



48
49
50
# File 'lib/random_strings.rb', line 48

def random_array_element(arr)
  arr[Random.rand*arr.size]
end

#random_digits(count = -1)) ⇒ Object



32
33
34
35
36
37
38
# 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_emailObject



52
53
54
# 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)) ⇒ Object



40
41
42
43
44
45
46
# 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_zipObject



56
57
58
# File 'lib/random_strings.rb', line 56

def random_zip
  return random_digits 5
end