Class: Chars::StringEnumerator Private
- Inherits:
-
Object
- Object
- Chars::StringEnumerator
- Includes:
- Enumerable
- Defined in:
- lib/chars/string_enumerator.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Enumerates through every possible string belonging to a character set and of a given length.
Instance Attribute Summary collapse
-
#char_set ⇒ CharSet
readonly
private
The character set to generate the strings from.
-
#length ⇒ Integer
readonly
private
The desired length of each string.
Instance Method Summary collapse
- #each {|string| ... } ⇒ Enumerator private
-
#initialize(char_set, length) ⇒ StringEnumerator
constructor
private
Initializes the string enumerator.
Constructor Details
#initialize(char_set, length) ⇒ StringEnumerator
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes the string enumerator.
33 34 35 36 |
# File 'lib/chars/string_enumerator.rb', line 33 def initialize(char_set,length) @char_set = char_set @length = length end |
Instance Attribute Details
#char_set ⇒ CharSet (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The character set to generate the strings from.
17 18 19 |
# File 'lib/chars/string_enumerator.rb', line 17 def char_set @char_set end |
#length ⇒ Integer (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The desired length of each string.
22 23 24 |
# File 'lib/chars/string_enumerator.rb', line 22 def length @length end |
Instance Method Details
#each {|string| ... } ⇒ Enumerator
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/chars/string_enumerator.rb', line 51 def each return enum_for(__method__) unless block_given? if @char_set.empty? return elsif @length == 0 yield "" return end chars = char_set.chars first_char = chars.first last_char = chars.last next_char = {} chars.each_cons(2) do |c1,c2| next_char[c1] = c2 end string = String.new(first_char * @length) last_index = @length - 1 loop do chars.each do |c| string[last_index] = c yield string.dup end last_index.downto(0) do |i| if string[i] == last_char string[i] = first_char if i == 0 return end else string[i] = next_char[string[i]] break end end end end |