Re: Mutually-Recursive Functions

From: bbiker [mailto:[email protected]]
Sent: Thursday, June 07, 2007 12:00 PM

[snip]

return alpha_col[0] - 'A'[0] + 1

[snip]

I thought I’d point out that
‘A’[0]
is equivalent to
?A
…and I personally find the latter to be much more friendly.

For completeness, here’s how I’d write the name-to-number conversion as
an auto-caching hash (without the bounds tests):

COL_NUM = Hash.new{ |h,alpha|
offset = ?A - 1
alpha.upcase!
case alpha.size
when 1
h[alpha] = alpha[0] - offset
when 2
h[alpha] = ( alpha[0] - offset ) * 26 +
( alpha[1] - offset )
end
}

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs