From: Adam Groves Date: 2006-03-01T23:08:37+09:00 Subject: Re: Indexing system - ruby newbie Hey thanks guys. I'm really enjoying learning ruby - especially because the ruby community is so helpful. Ross Bamford wrote: > On Wed, 2006-03-01 at 22:33 +0900, Dave Burt wrote: >> Try this: >> >> n = "" >> while(n > 0) >> s << ?A + n % 26 - 1 >> n /= 26 >> end >> n << "-" if n.empty? >> s.reverse >> >> Also, your @alphabet is ("a".."z").to_a > > This isn't as good as Dave's (it's potentially *lots* slower for a > start) but, well, I'm just an #inject addict really...: > > def letter(n) > (n < 1) ? '_' : (1...n).inject("A") { |curr, i| curr.succ } > end > > letter(0) > # => "_" > letter(1) > # => "A" > letter(10) > # => "J" > letter(27) > # => "AA" > letter(397) > # => "OG" -- Posted via http://www.ruby-forum.com/.