# A place for an "edit\_distance" method

**URL:** <https://www.ruby-forum.com/t/a-place-for-an-edit-distance-method/104577>\
**Category:** Ruby\
**Created:** [May 19, 2007, 8:31pm UTC](https://www.ruby-forum.com/t/a-place-for-an-edit-distance-method/104577 "2007-05-19T20:31:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2007, 8:31pm UTC](https://www.ruby-forum.com/t/a-place-for-an-edit-distance-method/104577/1 "2007-05-19T20:31:13Z")

</div>

Hi all,

In my application I need an “edit\_distance” method that computes the  
Edit Distance (Also called Levenshtein Distance, see this:  
[Levenshtein distance - Wikipedia](http://en.wikipedia.org/wiki/Levenshtein_distance)) between two  
strings. I wonder where is the right place to put this method. I have  
a “utils” Ruby file with various extensions to classes and modules  
that make my life easier. So there are two options:

1. Have a global (Kernel) method named “edit\_distance” that accepts  
two strings
2. Add a “edit\_distance\_from” method to the String class that accepts  
the “other” string

Which approach is better, in your opinion ?

P.S: There are lots of Edit Distance implementations in Ruby online.  
For example:  
[http://db.cs.helsinki.fi/~jaarnial/mt/archives/000074.html](http://db.cs.helsinki.fi/~jaarnial/mt/archives/000074.html)  
[http://ruby.brian-schroeder.de/editierdistanz/](http://ruby.brian-schroeder.de/editierdistanz/)

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/a-place-for-an-edit-distance-method/104577/2 "2007-05-19T20:50:14Z")

</div>

On Sun, May 20, 2007 at 03:30:15AM +0900, Eli B. wrote:

> the “other” string
> 
> Which approach is better, in your opinion ?

If you want to publish this for others to use, rather than just for your  
own  
private use, then neither of these is a good idea. I’d go with:

1. Make it a class method of your own distinct module

This avoids accidental pollution of namespace or system classes. Then,  
people can use MyUtils.edit\_distance(a,b), or if they want, can  
explicitly  
“include MyUtils” to be able to do “edit\_distance(a,b)” directly.

This is the approach taken by Ruby’s own Math library for functions like  
sqrt:

irb(main):001:0\> Math.sqrt(9)  
=\> 3.0  
irb(main):002:0\> include Math  
=\> Object  
irb(main):003:0\> sqrt(9)  
=\> 3.0

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/a-place-for-an-edit-distance-method/104577/3 "2007-05-19T20:55:14Z")

</div>

On 5/19/07, Eli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> two strings  
> Crouding the global namespace -1, but if you feel it is convenient.  
> 2) Add a “edit\_distance\_from” method to the String class that accepts  
> the “other” string  
> Why not, just that I believe extending core classes shall be done  
> under one of two conditions, (a) not an API just your program or (b)  
> just that API IOW, your package just does this and the extension is  
> well documented.

1. Create an extension module with a module method edit\_distance( str1,  
str2)

Cheers  
Robert
