Skip to content

Commit

Permalink
pulled out ffi_distance magic method out of private, because it wasn'…
Browse files Browse the repository at this point in the history
…t really made private
  • Loading branch information
harbu committed Dec 22, 2011
1 parent 86d03fe commit 596131a
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/levenshtein.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 596131a

Please sign in to comment.