rails/rails — pull request #15955 by JuanitoFatas (2014-06-28 07:26:23+00:00)
Title: Update the levenshtein distance method.

Copied from https://github.com/rails/rails/blob/master/railties/lib/rails/generators.rb#L258-L290.

Why I want to update? I update this because the current implementation has wrong result, if you copy this code into console:

```ruby
def distance(s1, s2)
s = s1.unpack('U*')
t = s2.unpack('U*')
m = s.length
n = t.length

# matrix initialization
d = []
0.upto(m) { |i| d << [i] }
0.upto(n) { |j| d[0][j] = j }

# distance computation
1.upto(m) do |i|
1.upto(n) do |j|
cost = s[i] == t[j] ? 0 : 1
d[i][j] = [
d[i-1][j] + 1, # deletion
d[i][j-1] + 1, # insertion
d[i-1][j-1] + cost, # substitution
].min
end
end

# all done
return d[m][n]
end
```

And type `distance 'kitten', 'sitting'`, the result is `2`, which is wrong. It should be `3`.