This is my array
arr = [1,2,3,4,5,6,7,8]
I want to write a method in ruby that would add 5 to each value in the array. How can I do the same ?
Please guide.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
Array#maplike this :See http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-map.
EDIT:
mapwill return a new array, if you want to modify this very array, usemap!even if I wouldn’t recommend it.