I’ve got a method that generates random strings:
def generate_letters(length)
chars = 'ABCDEFGHJKLMNOPQRSTUVWXYZ'
letters = ''
length.times { |i| letters << chars[rand(chars.length)] }
letters
end
I want to map values to generated strings, e.g.(1):
A = 1, B = 2, C = 3 , e.g.(2):
if I generate ACB it equals to 132. Any suggestions?
You can use that for concatenating these values:
and to sum them instead use
Enumerable#injectmethod (see docs, there are some nice examples):or
Enumerable#sumif you’re doing it inside Rails: