Skip to content

Instantly share code, notes, and snippets.

@dingsdax
Created January 9, 2011 19:42
Show Gist options
  • Save dingsdax/771943 to your computer and use it in GitHub Desktop.
Save dingsdax/771943 to your computer and use it in GitHub Desktop.
string similarity metrics in ruby
# http://staffwww.dcs.shef.ac.uk/people/S.Chapman/stringmetrics.html#hamming
def hamming_distance(str1, str2)
str1.split(//).zip(str2.split(//)).inject(0) { |h, e| e[0]==e[1] ? h+0 : h+1 }
end
@Mori
Copy link

Mori commented Oct 20, 2012

Nice. But odd that these are instance methods that take two args. Shouldn't they be class methods that take two args, or instance methods that take one arg?

@hakunin
Copy link

hakunin commented May 13, 2013

@Mori agreed. For those who don't want to invade String class here is my adaptation + usage https://gist.github.com/hakunin/5568313

@andreasgebhard7
Copy link

@dingsdax Thanks for sharing the two implementations, helped me a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment