Class: StringEnumerator
- Inherits:
-
Object
- Object
- StringEnumerator
- Defined in:
- lib/string_enumerator.rb,
lib/string_enumerator/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
- #end_mark ⇒ Object
-
#replacements ⇒ Object
readonly
Returns the value of attribute replacements.
- #start_mark ⇒ Object
Instance Method Summary collapse
-
#enumerate(str) ⇒ Object
Get all the possible enumerations of
str
given thereplacements
you defined. -
#initialize(replacements) ⇒ StringEnumerator
constructor
A new instance of StringEnumerator.
Constructor Details
#initialize(replacements) ⇒ StringEnumerator
Returns a new instance of StringEnumerator.
4 5 6 7 8 9 |
# File 'lib/string_enumerator.rb', line 4 def initialize(replacements) @replacements = replacements.inject({}) do |memo, (k, v)| memo[k.to_s] = v memo end end |
Instance Attribute Details
#end_mark ⇒ Object
19 20 21 |
# File 'lib/string_enumerator.rb', line 19 def end_mark @end_mark || ']' end |
#replacements ⇒ Object (readonly)
Returns the value of attribute replacements.
11 12 13 |
# File 'lib/string_enumerator.rb', line 11 def replacements @replacements end |
#start_mark ⇒ Object
14 15 16 |
# File 'lib/string_enumerator.rb', line 14 def start_mark @start_mark || '[' end |
Instance Method Details
#enumerate(str) ⇒ Object
Get all the possible enumerations of str
given the replacements
you defined
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/string_enumerator.rb', line 24 def enumerate(str) enumerations = [str] # seed it str.scan(pattern) do |placeholder| placeholder = placeholder[0] if r = replacements[placeholder] # "recursion" enumerations = enumerations.map do |e| r.map { |replacement| e.gsub(%{#{start_mark}#{placeholder}#{end_mark}}, replacement) } end.flatten end end enumerations end |