From: Daniel Martin Date: 2006-06-24T11:47:12+09:00 Subject: Re: [SUMMARY] Short But Unique (#83) Ruby Quiz writes: > Note that the last section of that method compares the pieces of the > string that will be left after the replacement with other strings in > the result set using the substring count method we examined earlier. > This code is there to prevent two strings from being compressed to > the same representation (though I doubt this weighted scoring system > is perfect). Actually, that's not why that code is there, or what it's doing. What it's doing is favoring unique strings over non-unique strings. For example: irb(main):002:0> %w(apple_juice orange_juice grape_juice prune_juice).compress(10) => ["apple...", "orange...", "grape...", "prune..."] irb(main):003:0> %w(orange_juice orange_marmalade orange_flavor orange_pulp).compress(10) => ["...juice", "...rmalade", "...flavor", "o...pulp"] The idea is that stuff common to all or almost-all of the original strings doesn't really help you when staring at a bunch of tabs. Abbreviating "orange_juice" as "orange..." makes sense when you have a bunch of juices, but not when you have a bunch of orange things. Avoiding previously used abbreviations is done with the bit: > if retval.include?(candidate) > record That is, if the to-be-returned list of abbreviations already includes this string, don't even score it and just assume that whatever else you had was better. This leads to bad results when all the possible abbreviations are already taken. Also, if you're going to allow for unicode in the output, you really should allow it in the input, and change all those .length to .jlength calls, but I'm still holding out for true transparent unicode support in ruby 2.0...