Increment Alphanumeric string in ruby for shorturl

Here a code i wrote for generating alphanumeric code in “youtube style”. This code automatically increment using a..z, A..Z, 0..9:

def generate_code

carry = false

for i in 1..(shorturl.length) do

shorturl = “aaZ9” #this is your starting string

index =  shorturl.length – i

if index ==  shorturl.length – 1

char = shorturl[index]

char = char + 1

if char == 123

char = 65

elsif char == 91

char = 48

elsif char == 58

char = 97

carry = true

end

shorturl[index] = char

elsif carry

carry = false

char = shorturl[index]

char = char + 1

if char == 123

char = 65

elsif char == 91

char = 48

elsif char == 58

char = 97

carry = true

end

shorturl[index] = char

end

end

if carry

shorturl = “a” + shorturl

end

puts shorturl

end