From: Adam <skidooer@gmail.com>
Date: November 25, 2006 1:13:25 AM CST
To: submission@rubyquiz.com
Subject: Ruby Quiz #103: DictionaryMatcherclass DictionaryMatcher < Array
attr_reader :optionsdef initialize(words = [], options = nil, lang = nil)
@options, @kcode = options, lang
super words
enddef =~(obj)
collect { |word| obj =~ Regexp.new(word, @options, @kcode) }.compact.first
end
alias_method :===, :=~
alias_method :match, :=~def casefold?
options & Regexp::IGNORECASE
enddef kcode
Regexp.new('', @options, @kcode).kcode
enddef to_s
join '|'
end
alias_method :source, :to_sdef inspect
Regexp.new('REPLACE', @options, @kcode).inspect.sub('REPLACE', to_s)
end
endclass String
alias_method :match_object, :=~def =~(obj)
result = match_object(obj)
obj.is_a?(DictionaryMatcher) ? !result.nil? : result
end
end------
require 'test/unit'
require 'dictionary_matcher'class DictionaryMatcherTest < Test::Unit::TestCase
def setup
@dictionary = DictionaryMatcher.new
@dictionary << "string"
@dictionary << "Ruby"
enddef test_added_terms
assert @dictionary.include?("Ruby")
assert ! @dictionary.include?("missing")
assert ! @dictionary.include?("stringing you along")
enddef test_regexp_like_seach
assert_equal 5, @dictionary =~ "long string"
assert_equal nil, @dictionary =~ "rub you the wrong way"
assert_equal true, "long string" =~ @dictionary
enddef test_case_sesitive_match
assert ! @dictionary.casefold?
assert_not_equal 5, @dictionary =~ "lONg sTrINg"
assert_not_equal true, "lonG STRing" =~ @dictionary
enddef test_case_insesitive_match
@dictionary.instance_variable_set('@options', Regexp::IGNORECASE)assert @dictionary.casefold?
assert_equal 5, @dictionary =~ "lONg sTrINg"
assert_equal true, "lonG STRing" =~ @dictionary
enddef test_new_dictionary_matcher_with_options
dictionary = DictionaryMatcher.new(%W( here be words ),
Regexp::EXTENDED | Regexp::IGNORECASE, 'u')assert dictionary
assert_equal %W( words be here ).sort, dictionary.sort
assert_equal Regexp::EXTENDED | Regexp::IGNORECASE, dictionary.options
assert_equal 'utf8', dictionary.kcode
enddef test_regexp_compatibility_methods
assert @dictionary.eql?(@dictionary)
assert ! @dictionary.casefold?
assert @dictionary.kcode.nil?
assert @dictionary.options.nil?
assert_equal @dictionary.to_s, @dictionary.source
assert_equal 'string|Ruby', @dictionary.source
assert_equal '/string|Ruby/', @dictionary.inspect
end
end
···
Begin forwarded message: