Forum: Ruby A place for an "edit_distance" method

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Fc761ccaf6c0d7d977e2959f9bfebd06?d=identicon&s=25 Eli Bendersky (eliben)
on 2007-05-19 20:31
(Received via mailing list)
Hi all,

In my application I need an "edit_distance" method that computes the
Edit Distance (Also called Levenshtein Distance, see this: 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:
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-05-19 20:50
(Received via mailing list)
On Sun, May 20, 2007 at 03:30:15AM +0900, Eli Bendersky 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
private use, then neither of these is a good idea. I'd go with:

3) 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
"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

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


703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-05-19 20:55
(Received via mailing list)
On 5/19/07, Eli Bendersky <> 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.

3) Create an extension module with a module method edit_distance( str1,

This topic is locked and can not be replied to.