# Ruby Quiz #103: DictionaryMatcher

**URL:** <https://rubytalk.org/t/ruby-quiz-103-dictionarymatcher/33236>\
**Category:** ruby-talk\
**Created:** [27 November 2006 04:25 UTC](https://rubytalk.org/t/ruby-quiz-103-dictionarymatcher/33236 "2006-11-27T04:25:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [27 November 2006 04:25 UTC](https://rubytalk.org/t/ruby-quiz-103-dictionarymatcher/33236/1 "2006-11-27T04:25:27Z")

</div>

> From: Adam \<skidooer@gmail.com\>  
> Date: November 25, 2006 1:13:25 AM CST  
> To: submission@rubyquiz.com  
> Subject: Ruby Quiz #103: DictionaryMatcher
> 
> class DictionaryMatcher \< Array  
> attr\_reader :options
> 
> def initialize(words = , options = nil, lang = nil)  
> &nbsp;&nbsp;&nbsp;@options, @kcode = options, lang  
> &nbsp;&nbsp;&nbsp;super words  
> end
> 
> def =~(obj)  
> &nbsp;&nbsp;&nbsp;collect { |word| obj =~ Regexp.new(word, @options, @kcode) }.compact.first  
> end  
> alias\_method :===, :=~  
> alias\_method :match, :=~
> 
> def casefold?  
> &nbsp;&nbsp;&nbsp;options & Regexp::IGNORECASE  
> end
> 
> def kcode  
> &nbsp;&nbsp;&nbsp;Regexp.new('', @options, @kcode).kcode  
> end
> 
> def to\_s  
> &nbsp;&nbsp;&nbsp;join '|'  
> end  
> alias\_method :source, :to\_s
> 
> def inspect  
> &nbsp;&nbsp;&nbsp;Regexp.new('REPLACE', @options, @kcode).inspect.sub('REPLACE', to\_s)  
> end  
> end
> 
> class String  
> alias\_method :match\_object, :=~
> 
> def =~(obj)  
> &nbsp;&nbsp;&nbsp;result = match\_object(obj)  
> &nbsp;&nbsp;&nbsp;obj.is\_a?(DictionaryMatcher) ? !result.nil? : result  
> end  
> end
> 
> ------
> 
> require 'test/unit'  
> require 'dictionary\_matcher'
> 
> class DictionaryMatcherTest \< Test::Unit::TestCase  
> def setup  
> &nbsp;&nbsp;&nbsp;@dictionary = DictionaryMatcher.new  
> &nbsp;&nbsp;&nbsp;@dictionary \<\< "string"  
> &nbsp;&nbsp;&nbsp;@dictionary \<\< "Ruby"  
> end
> 
> def test\_added\_terms  
> &nbsp;&nbsp;&nbsp;assert @dictionary.include?("Ruby")  
> &nbsp;&nbsp;&nbsp;assert ! @dictionary.include?("missing")  
> &nbsp;&nbsp;&nbsp;assert ! @dictionary.include?("stringing you along")  
> end
> 
> def test\_regexp\_like\_seach  
> &nbsp;&nbsp;&nbsp;assert\_equal 5, @dictionary =~ "long string"  
> &nbsp;&nbsp;&nbsp;assert\_equal nil, @dictionary =~ "rub you the wrong way"  
> &nbsp;&nbsp;&nbsp;assert\_equal true, "long string" =~ @dictionary  
> end
> 
> def test\_case\_sesitive\_match  
> &nbsp;&nbsp;&nbsp;assert ! @dictionary.casefold?  
> &nbsp;&nbsp;&nbsp;assert\_not\_equal 5, @dictionary =~ "lONg sTrINg"  
> &nbsp;&nbsp;&nbsp;assert\_not\_equal true, "lonG STRing" =~ @dictionary  
> end
> 
> def test\_case\_insesitive\_match  
> &nbsp;&nbsp;&nbsp;@dictionary.instance\_variable\_set('@options', Regexp::IGNORECASE)
> 
> &nbsp;&nbsp;&nbsp;assert @dictionary.casefold?  
> &nbsp;&nbsp;&nbsp;assert\_equal 5, @dictionary =~ "lONg sTrINg"  
> &nbsp;&nbsp;&nbsp;assert\_equal true, "lonG STRing" =~ @dictionary  
> end
> 
> def test\_new\_dictionary\_matcher\_with\_options  
> &nbsp;&nbsp;&nbsp;dictionary = DictionaryMatcher.new(%W( here be words ),  
> Regexp::EXTENDED | Regexp::IGNORECASE, 'u')
> 
> &nbsp;&nbsp;&nbsp;assert dictionary  
> &nbsp;&nbsp;&nbsp;assert\_equal %W( words be here ).sort, dictionary.sort  
> &nbsp;&nbsp;&nbsp;assert\_equal Regexp::EXTENDED | Regexp::IGNORECASE, dictionary.options  
> &nbsp;&nbsp;&nbsp;assert\_equal 'utf8', dictionary.kcode  
> end
> 
> def test\_regexp\_compatibility\_methods  
> &nbsp;&nbsp;&nbsp;assert @dictionary.eql?(@dictionary)  
> &nbsp;&nbsp;&nbsp;assert ! @dictionary.casefold?  
> &nbsp;&nbsp;&nbsp;assert @dictionary.kcode.nil?  
> &nbsp;&nbsp;&nbsp;assert @dictionary.options.nil?  
> &nbsp;&nbsp;&nbsp;assert\_equal @dictionary.to\_s, @dictionary.source  
> &nbsp;&nbsp;&nbsp;assert\_equal 'string|Ruby', @dictionary.source  
> &nbsp;&nbsp;&nbsp;assert\_equal '/string|Ruby/', @dictionary.inspect  
> end  
> end

> **···**
>
> Begin forwarded message:
