From: Robert Klemme Date: 2007-07-14T20:25:10+09:00 Subject: Re: Base 26 On 13.07.2007 22:37, Stefan Rusterholz wrote: > Mike Moore wrote: >> Hey friends! What is the best way to convert a Numeric to a string >> encoded >> with base 26? I want the string to contain just alpha characters, not >> alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p', >> 'a-z') >> and I'm wondering if there is a more concise way. I think your solution is pretty concise already. You could however do something like this: class Integer C26 = (?a..?z).to_a.freeze def to_b26 x = self s = "" until x == 0 m = x % 26 s << C26[m] x = (x - m) / 26 end s == "" ? "0" : s.reverse! end end Or, a more generic solution: class Integer def to_base(chars) raise ArgumentError if chars.empty? return chars[0].chr if self == 0 x = self s = "" until x == 0 m = x % chars.size s << chars[m] x = (x - m) / chars.size end s.reverse! end end Now you can do irb(main):065:0> 1.to_base( (?a..?z).to_a ) => "b" irb(main):066:0> 26.to_base( (?a..?z).to_a ) => "ba" And you can even use a String as argument - duck typing at its best! irb(main):067:0> 7.to_base "01" => "111" irb(main):068:0> 6.to_base "01" => "110" Kind regards robert