From fd987f27bb47b30685349daa02367f0662c1625e Mon Sep 17 00:00:00 2001 From: Kiyoka Nishiyama Date: Tue, 14 Feb 2012 13:23:05 +0900 Subject: [PATCH] Supported pure?() method. Supported `fallback to pure` feature on JRuby. --- lib/fuzzystringmatch.rb | 6 +++++- lib/fuzzystringmatch/inline/jarowinkler.rb | 4 ++++ lib/fuzzystringmatch/pure/jarowinkler.rb | 4 ++++ test/basic_native_spec.rb | 2 ++ test/basic_pure_spec.rb | 2 ++ test/mutibyte_spec.rb | 2 ++ 6 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/fuzzystringmatch.rb b/lib/fuzzystringmatch.rb index 0eb87af..8e7f38c 100644 --- a/lib/fuzzystringmatch.rb +++ b/lib/fuzzystringmatch.rb @@ -17,7 +17,11 @@ # require 'fuzzystringmatch/pure' begin - require 'fuzzystringmatch/inline' + if RUBY_PLATFORM == "java" + STDERR.puts "Warning: native version is disabled on java platform. falled back to pure ruby version..." + else + require 'fuzzystringmatch/inline' + end rescue LoadError end diff --git a/lib/fuzzystringmatch/inline/jarowinkler.rb b/lib/fuzzystringmatch/inline/jarowinkler.rb index 89a09e8..fd27258 100644 --- a/lib/fuzzystringmatch/inline/jarowinkler.rb +++ b/lib/fuzzystringmatch/inline/jarowinkler.rb @@ -18,6 +18,10 @@ module FuzzyStringMatch require 'inline' class JaroWinklerInline + def pure? + false + end + inline do |builder| builder.include '' builder.add_compile_flags '-x c++', '-lstdc++' diff --git a/lib/fuzzystringmatch/pure/jarowinkler.rb b/lib/fuzzystringmatch/pure/jarowinkler.rb index 60e4373..3b66de3 100644 --- a/lib/fuzzystringmatch/pure/jarowinkler.rb +++ b/lib/fuzzystringmatch/pure/jarowinkler.rb @@ -19,6 +19,10 @@ module FuzzyStringMatch class JaroWinklerPure THRESHOLD = 0.7 + def pure? + true + end + def getDistance( s1, s2 ) a1 = s1.split( // ) a2 = s2.split( // ) diff --git a/test/basic_native_spec.rb b/test/basic_native_spec.rb index 674931a..fa7347f 100644 --- a/test/basic_native_spec.rb +++ b/test/basic_native_spec.rb @@ -46,5 +46,7 @@ d2 = @jarow.getDistance("brittney spears", "brittney startzman") d1 > d2 }.should be_true + + @jarow.pure?( ).should == (RUBY_PLATFORM == "java") end end diff --git a/test/basic_pure_spec.rb b/test/basic_pure_spec.rb index 66eaf75..fad6f94 100644 --- a/test/basic_pure_spec.rb +++ b/test/basic_pure_spec.rb @@ -46,5 +46,7 @@ d2 = @jarow.getDistance("brittney spears", "brittney startzman") d1 > d2 }.should be_true + + @jarow.pure?( ).should be_true end end diff --git a/test/mutibyte_spec.rb b/test/mutibyte_spec.rb index 07a84d2..c0ef409 100644 --- a/test/mutibyte_spec.rb +++ b/test/mutibyte_spec.rb @@ -52,5 +52,7 @@ @jarow.getDistance( "まつもとゆきひろ", "まつもとひろゆき" ).should be_within(0.0001).of(0.9500) @jarow.getDistance( "クライエント", "クライアント" ).should be_within(0.0001).of(0.9222) @jarow.getDistance( "サーバー", "サーバ" ).should be_within(0.0001).of(0.9416) + + @jarow.pure?( ).should be_true end end