Class: LooseTightDictionary::Tightener

Inherits:
Object
  • Object
show all
Defined in:
lib/loose_tight_dictionary/tightener.rb

Overview

A tightener just strips a string down to its core

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#regexpObject (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

Returns:

  • (Boolean)


11
12
13
# File 'lib/loose_tight_dictionary/tightener.rb', line 11

def apply?(str)
  !!(regexp.match(str))
end

#inspectObject



24
25
26
# File 'lib/loose_tight_dictionary/tightener.rb', line 24

def inspect
  "#<Tightener regexp=#{regexp.inspect}>"
end