I would like to extract an element from an array, predictably based on a string input.
My use case: Generate a color for a user, based on name.
One (faulty) implementation would be to generate a hash from the user name, and reduce it to a value between 0-25 like so:
def color
colors = array_of_25_colors
colors[Digest::MD5.hexdigest(name).to_i(16) & 25]
end
This implementation only returns 8 possible colors, so no good.
EDIT
Another implementation I've tried is to use the byte code from the first character in the name, but since I allow unicode in names this is not practical:
"aaron".first.bytes.first - 97
=> 0 #Okay!
"zac".first.bytes.first - 97
=> 25 #Nice!
"örjan".first.bytes.first - 97
=> 98 #Doh!
What could a working implementation look like?