Class: Chars::StringEnumerator Private

Inherits:
Object
  • Object
show all
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.

Since:

  • 0.3.0

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • char_set (Chars::CharSet)

    The character set to generate the strings from.

  • length (Integer)

    The desired length of each string.

Since:

  • 0.3.0



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_setCharSet (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.

Returns:

Since:

  • 0.3.0



17
18
19
# File 'lib/chars/string_enumerator.rb', line 17

def char_set
  @char_set
end

#lengthInteger (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.

Returns:

Since:

  • 0.3.0



22
23
24
# File 'lib/chars/string_enumerator.rb', line 22

def length
  @length
end

Instance Method Details

#each {|string| ... } ⇒ Enumerator

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.

Enumerates through every possible string belonging to #char_set and #length long.

Yields:

  • (string)

    The given block will be passed each sequential string.

Yield Parameters:

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.

Since:

  • 0.3.0



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