How do I set a starting offset for each loop in ruby?
I want the loop to begin from a[3] instead of a[0]. How do I set that?
a = [ab, cd, ef, gh, hi, jk]
a.each do |i|
#some stuff
end
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.
Another, possibly more direct and readable possibility is to use
Array#drop:Now this really shines if combined with other methods inherited from
Enumerable, so chances are there’s a better alternative to your imperativeeachloop. Say you want to filter the extracted slice and transform it afterwards:Or say you want to print a list of all the values:
Output:
These features inherited from functional programming are what makes Ruby so strong 🙂