October 2006
Intermediate to advanced
888 pages
16h 55m
English
Ruby has built-in ideas about comparing strings; comparisons are done lexicographically as we have come to expect (that is, based on character set order). But if we want, we can introduce rules of our own for string comparisons, and these can be of arbitrary complexity.
For example, suppose that we want to ignore the English articles a, an, and the at the front of a string, and we also want to ignore most common punctuation marks. We can do this by overriding the built-in method <=> (which is called for <, <=, >, and >=). Listing 2.1 shows how we do this.
class String alias old_compare <=> def <=>(other) a = self.dup b = other.dup # Remove punctuation ... |
Read now
Unlock full access