Skip to content

Instantly share code, notes, and snippets.

@ivanacostarubio
Created September 2, 2009 01:41
Show Gist options
  • Save ivanacostarubio/179489 to your computer and use it in GitHub Desktop.
Save ivanacostarubio/179489 to your computer and use it in GitHub Desktop.
http://en.wikipedia.org/wiki/Levenshtein_distance
[root@linux-server test]# irb
irb(main):001:0>
Create a list of values contained in an array object:
myList = ['ruby','sql','ruby','bash','python','perl','java','sql']
=> ["ruby", "sql", "ruby", "bash", "python", "perl", "java", "sql"]
When running irb the second line displays the value of the expression as it is evaluated. This list of values is analogous to the results of the ls command used in previous examples. This will serve as our initial list that will be sorted and limited.
The list can now be sorted:
irb(main):002:0> myList.sort
=> ["bash", "java", "perl", "python", "ruby", "ruby", "sql", "sql"]
A list of unique results can be returned:
irb(main):003:0> myList.uniq
=> ["ruby", "sql", "bash", "python", "perl", "java"]
There is even a grep method complete with regular expression matching capabilities:
irb(main):004:0> myList.grep(/r/)
=> ["ruby", "ruby", "perl"]
You can also simulate the uniq -c option by calling the size method on the array returned by the uniq method:
irb(main):005:0> myList.uniq.size
=> 6
eql?
- determines whether two object references refer to objects that
have the same "value"
- for example, my_car.eql?(your_car)
could test whether my car and your car have the same make, model and year
- need to override this method in the Car class to be meaningful,
otherwise it's the same as ==
equal?
- determines whether two object references refer to the same object in memory
(have the same object id)
- for example, my_car.equal?(your_car)
tests whether we share the same car
==
- sometimes same as equal?, but sometimes different
- for example, mixing in the Comparable module changes it to be based on
<=> which would be overridden
I kind of wish that "eql?" was named "same_value" and "equal?" was
named "same_object?" so it would be easier to remember. Using "=="
seems somewhat dangerous unless you're working with built-in types or
types that you know mixin Comparable. Otherwise there is some
uncertainty about what it does without looking at the code of the
classes being compared.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment