Class: Girbot::StringGenerator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStringGenerator

Returns a new instance of StringGenerator.



5
6
7
# File 'lib/girbot/string_generator.rb', line 5

def initialize
  @items = ('!'..'~').to_a
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



3
4
5
# File 'lib/girbot/string_generator.rb', line 3

def items
  @items
end

Instance Method Details

#repeated_permutation(min_length = nil, max_length = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/girbot/string_generator.rb', line 13

def repeated_permutation min_length=nil, max_length=nil
  items = []
  min_length = 1 if min_length.nil?
  max_length = @items.size if max_length.nil?
  raise 'min is greater than max' if min_length > max_length

  (min_length..max_length).each do |range|
    items.concat repeated_permutation_with_length(range)
  end

  items
end

#repeated_permutation_with_length(length) ⇒ Object



26
27
28
# File 'lib/girbot/string_generator.rb', line 26

def repeated_permutation_with_length length
  stringify @items.repeated_permutation(length).to_a
end

#stringify(array) ⇒ Object



30
31
32
# File 'lib/girbot/string_generator.rb', line 30

def stringify array
  array.map { |e| e.join('') }.uniq.reject { |c| c.empty? }
end