From: gabriele renzi Date: 2004-11-06T23:38:40+09:00 Subject: Re: Succ distance between two strings trans. (T. Onoma) ha scritto: > Is there an efficient way to calculate the "succ distance" between two strings > (without iterating over each succession)? Examples: > > 'a'..'a' 0 > 'a'..'b' 1 > 'a'..'z' 25 > 'a'..'aa' 26 > > T. > > my tentativ impl would be class String def value i=-26 split(//).inject(0){|x,y| i+=26 x+y[0]-?a+i } end end class Range def dist max.value - min.value end end p 'aa'.value - 'a'.value p 'a'.value - 'a'.value p ('a'..'b').dist p ('a'..'z').dist note that '-?a' could be moved in the 'i' initialization but I'm running away :)