Class: LooseTightDictionary::Tightener
- Inherits:
-
Object
- Object
- LooseTightDictionary::Tightener
- Defined in:
- lib/loose_tight_dictionary/tightener.rb
Overview
A tightener just strips a string down to its core
Instance Attribute Summary collapse
-
#regexp ⇒ Object
readonly
Returns the value of attribute regexp.
Instance Method Summary collapse
-
#apply(str) ⇒ Object
The result of applying a tightener is just all the captures put together.
-
#apply?(str) ⇒ Boolean
A tightener applies when its regexp matches and captures a new (shorter) string.
-
#initialize(regexp_or_str) ⇒ Tightener
constructor
A new instance of Tightener.
- #inspect ⇒ Object
Constructor Details
#initialize(regexp_or_str) ⇒ Tightener
Returns a new instance of Tightener.
6 7 8 |
# File 'lib/loose_tight_dictionary/tightener.rb', line 6 def initialize(regexp_or_str) @regexp = regexp_or_str.to_regexp end |
Instance Attribute Details
#regexp ⇒ Object (readonly)
Returns the value of attribute regexp.
4 5 6 |
# File 'lib/loose_tight_dictionary/tightener.rb', line 4 def regexp @regexp end |
Instance Method Details
#apply(str) ⇒ Object
The result of applying a tightener is just all the captures put together.
16 17 18 19 20 21 22 |
# File 'lib/loose_tight_dictionary/tightener.rb', line 16 def apply(str) if match_data = regexp.match(str) match_data.captures.join else str end end |
#apply?(str) ⇒ Boolean
A tightener applies when its regexp matches and captures a new (shorter) string
11 12 13 |
# File 'lib/loose_tight_dictionary/tightener.rb', line 11 def apply?(str) !!(regexp.match(str)) end |
#inspect ⇒ Object
24 25 26 |
# File 'lib/loose_tight_dictionary/tightener.rb', line 24 def inspect "#<Tightener regexp=#{regexp.inspect}>" end |