Sha256: 99477e12ade7fe41e960db55d874f1f41e2e0cc8a8684829446d79a53669ad6f

Contents?: true

Size: 660 Bytes

Versions: 9

Compression:

Stored size: 660 Bytes

Contents

class LooseTightDictionary
  # A tightener just strips a string down to its core
  class Tightener
    attr_reader :regexp
    
    def initialize(regexp_or_str)
      @regexp = regexp_or_str.to_regexp
    end
    
    # A tightener applies when its regexp matches and captures a new (shorter) string
    def apply?(str)
      !!(regexp.match(str))
    end
    
    # The result of applying a tightener is just all the captures put together.
    def apply(str)
      if match_data = regexp.match(str)
        match_data.captures.join
      else
        str
      end
    end
    
    def inspect
      "#<Tightener regexp=#{regexp.inspect}>"
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
loose_tight_dictionary-1.0.5 lib/loose_tight_dictionary/tightener.rb
loose_tight_dictionary-1.0.4 lib/loose_tight_dictionary/tightener.rb
loose_tight_dictionary-1.0.3 lib/loose_tight_dictionary/tightener.rb
loose_tight_dictionary-1.0.2 lib/loose_tight_dictionary/tightener.rb
loose_tight_dictionary-1.0.1 lib/loose_tight_dictionary/tightener.rb
loose_tight_dictionary-1.0.0 lib/loose_tight_dictionary/tightener.rb
loose_tight_dictionary-0.2.3 lib/loose_tight_dictionary/tightener.rb
loose_tight_dictionary-0.2.2 lib/loose_tight_dictionary/tightener.rb
loose_tight_dictionary-0.2.1 lib/loose_tight_dictionary/tightener.rb