diff --git a/lib/levenshtein.rb b/lib/levenshtein.rb index b9def8f..f2f120d 100644 --- a/lib/levenshtein.rb +++ b/lib/levenshtein.rb @@ -9,19 +9,21 @@ class << self candidates = ['.bundle', '.so', '.dylib', ''].map { |ext| library + ext } ffi_lib(candidates) + # Safe version of distance, checks that arguments are really strings. def distance(str1, str2) validate(str1) validate(str2) ffi_distance(str1, str2) end + # Unsafe version. Results in a segmentation fault if passed nils! + attach_function :ffi_distance, :levenshtein, [:string, :string], :int + private def validate(arg) unless arg.kind_of?(String) raise TypeError, "wrong argument type #{arg.class} (expected String)" end end - - attach_function :ffi_distance, :levenshtein, [:string, :string], :int end end