Class: Passweird::Substringer
- Inherits:
-
Object
- Object
- Passweird::Substringer
- Defined in:
- lib/passweird/substringer.rb
Overview
The Substringer class is responsible for generating all unique substrings of a given root string with a specified minimum length.
Example usage:
substringer = Passweird::Substringer.new("example")
substrings = substringer.substrings(min_length: 3)
# => ["exa", "exam", "examp", "exampl", "example", "xam", "xamp", "xampl", "xample", ...]
Instance Attribute Summary collapse
-
#root_string ⇒ Object
readonly
Returns the value of attribute root_string.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(root_string) ⇒ Substringer
constructor
A new instance of Substringer.
-
#substrings(min_length: 3) ⇒ Array<String>
Generates all unique substrings of the root_string with a minimum length.
Constructor Details
#initialize(root_string) ⇒ Substringer
Returns a new instance of Substringer.
18 19 20 21 22 |
# File 'lib/passweird/substringer.rb', line 18 def initialize(root_string) raise ArgumentError, "root_string must be a String" unless root_string.is_a?(String) @root_string = root_string end |
Instance Attribute Details
#root_string ⇒ Object (readonly)
Returns the value of attribute root_string.
12 13 14 |
# File 'lib/passweird/substringer.rb', line 12 def root_string @root_string end |
Class Method Details
.substrings(root_string, min_length: 3) ⇒ Object
14 15 16 |
# File 'lib/passweird/substringer.rb', line 14 def self.substrings(root_string, min_length: 3) new(root_string).substrings(min_length: min_length) end |
Instance Method Details
#substrings(min_length: 3) ⇒ Array<String>
Generates all unique substrings of the root_string with a minimum length
28 29 30 31 32 33 34 35 |
# File 'lib/passweird/substringer.rb', line 28 def substrings(min_length: 3) raise ArgumentError, "min_length must be an Integer" unless min_length.is_a?(Integer) raise ArgumentError, "min_length must be greater than 0" if min_length <= 0 return [root_string] if root_string.length < min_length ([root_string] + get_substrings(min_length)).uniq end |