On Wed, 2006-03-01 at 22:33 +0900, Dave Burt wrote:
> Adam Groves wrote:
> > Hi there,
> >
> > I'm trying to write a class which converts a number into letters like
> > so:
> > 0  => -
> > 1  => A
> > 10 => J
> > 27 => AA
> 
> 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"

-- 
Ross Bamford - rosco / roscopeco.REMOVE.co.uk