From: Logan Capaldo Date: 2006-03-02T06:43:01+09:00 Subject: Re: Indexing system - ruby newbie On Mar 1, 2006, at 12:43 PM, Adam Shelly wrote: >> OP wanted: >> >> ... 24 25 26 27 28 29 ... >> ... 'X', 'Y', 'Z', 'AA', 'AB', 'AC' ... > > Here's one that uses succ to do the dirty work, but doesn't complicate > things with inject: > (note that '@'.succ = 'A') > > def letter n > l='@' > n.times{l.succ!} > l.gsub(/@/,'-') > end > > -Adam > And now for the over-engineered approach to balance out the golfing : % cat indexer.rb class Indexer def initialize @index_cache = ('A'..'Z').to_a @index_cache.unshift('-') end def alpha_index(i) if res = @index_cache[i] res else @index_cache[i] = alpha_index(i - 1).succ end end alias [] alpha_index end if $0 == __FILE__ idx = Indexer.new puts idx.alpha_index(27) puts idx.alpha_index(0) puts idx[26] end % ruby indexer.rb AA - Z