Developing a file name generator for imgur so I can cataloge private images not submitted to the gallery.
Code
def generateFileName(currentFileName)
currentFileName = currentFileName.to_i(36)
currentFileName = currentFileName.next
currentFileName.to_s(36)
end
At the moment it supports 0-9 a-z file names and tomorrow I will implement A-Z.
This works via changing bases of integers. Here's a basic rundown.
1) Pass "abc" to generateFileName
2) Convert currentFileName to base 10 from base 36 (currentFileName now equals 13368)
Code
-Calculation as follows
| 2 | 1 | 0 |
a b c
c in base 36 = 12
b in base 36 = 11
a in base 36 = 10
c is in slot 0 so 12*(36^0)
b is in slot 1 so 11*(36^1)
a is in slot 2 so 10*(36^2)
add them up and the answer is 13368
3) From here we call the .next() method which increments the right most place and if it overflows (IE: if it was 9 and rolls over to 0) it carries over a digit to the next left place until there is no more carries/overflows. (currentFileName now equals 13369)
4) We convert back from base 10 to base 36 (currentFileName now equals "abd")
5) Because of the way ruby works the last statement in any function is automatically its return value so when we started we inputted "abc" and the function returned "abd"
This is the simplest method you can use to sequentially generate alpha numeric strings in any language.
A final example would to start at "a9z" and call the function 4 times passing the previously returned variable. The output would be
Code
a9z
aa0
aa1
aa2
This post was edited by AbDuCt on Feb 14 2014 04:27am