Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign number to alphabet in array

Tags:

arrays

ruby

What I want to do is use an array.each to turn each letter into its corresponding letter in the alphabet plus nine. I know it's possible to put it against a case going through all 26 letters, but that seems unnecessary.

For example. Say the array is:

["N", "O", "T", "H", "I", "N", "G"]

I want the array to become

["23", "24", "29", "17", "19", "24", "17"]

like image 262
Bluethousandand2 Avatar asked Oct 19 '25 09:10

Bluethousandand2


1 Answers

Here's one way:

arr.map {|ch| ch.ord - 'A'.ord + 10}

like image 74
rohit89 Avatar answered Oct 22 '25 02:10

rohit89



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!