Random Snippet: Soundex
This bit of code is the Soundex algorithm based on some C written by a friend of mine (Hi Jen!). She writes really compact C (read: hard to read) and it took me a long time to understand what the code she wrote did.
So here is the annotated version of the code (correlates to the Rule numbers on Wikipedia).
RUBY:
-
class String
-
@@sem = [ 0,1,2,3,0,1,2,0,0,2,2,4,5,5,0,1,2,6,2,3,0,1,0,2,0,2 ] # Letter codes, Rule 3
-
-
def to_soundex
-
s = self.upcase
-
chars = s[1 .. s.size - 1].split(//) # Split the word into characters to encode
-
ret = s[0,1] # Rule 1
-
chars.each_index do |i|
-
c = chars[i]
-
next if ( c <'A' or c> 'Z' ) # Not a letter
-
next if i>0 and c == chars[i - 1] # Rule 4
-
d = @@sem[ c[0] - 'A'[0] ] # Encode it
-
ret += d.to_s unless d == 0 # Rule 2
-
end
-
ret += "0000" # Rule 5
-
ret[0..3] # Rule 5
-
end
-
end
It was fun to write and it works well.

