R
Roberto Decurnex Gorosito
I am have an String object like "12345678", and an Array object like
[ 8,7,6,5,4,3,2,1].
The main idea is:
myStr = '12345678'
myArray = [8,7,6,5,4,3,2,1]
total = 0
for i in 0..7
total+= myStr[ i ].to_i * myArray[ i ]
end
The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
i can' t get it
Like a quick solution i have to do this:
myStr = '12345678'
myArray = [8,7,6,5,4,3,2,1]
total = 0
for i in 0..7
total += ( String.new << myStr[ i ] ).to_i * myArray[ i ]
end
It's so horrible!!!!!
Please, help my code to be beautiful as ruby is
[ 8,7,6,5,4,3,2,1].
The main idea is:
myStr = '12345678'
myArray = [8,7,6,5,4,3,2,1]
total = 0
for i in 0..7
total+= myStr[ i ].to_i * myArray[ i ]
end
The awaited result is 120 but coz myStr[ i ] returns the ascii fixnum
i can' t get it
Like a quick solution i have to do this:
myStr = '12345678'
myArray = [8,7,6,5,4,3,2,1]
total = 0
for i in 0..7
total += ( String.new << myStr[ i ] ).to_i * myArray[ i ]
end
It's so horrible!!!!!
Please, help my code to be beautiful as ruby is