Skip to content

Instantly share code, notes, and snippets.

@brainopia
Created July 14, 2009 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save brainopia/147217 to your computer and use it in GitHub Desktop.
Save brainopia/147217 to your computer and use it in GitHub Desktop.
require 'digest/sha1'
class String
def hash
Digest::SHA1.hexdigest self
end
def bit
"%0160b" % to_i(16)
end
end
def distance(word1, word2)
distance = 0
word1.chars.zip(word2.chars) do |char1, char2|
distance += 1 unless char1 == char2
end
return distance
end
test_phrase = "Rubinius one eight six active active record memcached exception JRuby DHH TOKYO sdfe3"
key_phrase = "I am not a big believer in fortune telling"
h1 = test_phrase.hash
h2 = key_phrase.hash
b1 = h1.bit
b2 = h2.bit
p distance b1, b2 # => 74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment