From: Adam Groves Date: 2006-03-02T00:14:47+09:00 Subject: Re: Indexing system - ruby newbie Dear Ross, it works a treat but I'm having a bit of trouble figuring out what's going on. (n < 1) ? '_' : (1...n).inject("A") { |curr, i| curr.succ } I get this: if n<1 '_' else But I'm stuck here. (1...n).inject("A") { |curr, i| curr.succ} I still can't quite get my head around blocks beyond .each do |x| 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/.