Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert an array of string integers to an array of integers? Ruby

I have an array:

all_ages = ["11", "9", "10", "8", "9"]

and I want to convert it to an array of integers to hopefully make it easier to add them all together.
Any help greatly appreciated, looking for level one solutions.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

all_ages = ["11", "9", "10", "8", "9"]

Code

p all_ages.map(&:to_i).sum

Or

p all_ages.map { |x| x.to_i }.sum

Output

47
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading