2012-11-27 82 views

回答

9

這裏有一個遞歸散列將處理索引你:

index_hash = Hash.new {|hash,key| hash[key] = hash[key - 1].next }.merge({0 => "A"}) 
index_hash[26] #=> "AA" 

這裏的關鍵是.next方法,它在發送給一個字符串,將返回字母下面的字符串,例如"CD".next #=> "CE"

您能澄清您的第一個問題嗎?

2
class Numeric 
    Alph = ("A".."Z").to_a 
    def alph 
    s, q = "", self 
    (q, r = (q - 1).divmod(26)) && s.prepend(Alph[r]) until q.zero? 
    s 
    end 
end 

(26+1).alph #=> "AA"