For instance, I have a
arr = [1,2,3,4]
If I call arr.each, I will access:
1
2
3
4
But I want
1 2
2 3
3 4
Is it possible with built-in function? If not, what’s the best practice?
Another question: if I want 1 2 and 3 4?
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 probably want to look at each_cons for your first case:
For your second case (wanting sets of elements) you would use each_slice:
Either of these methods accepts a single integer specifying the size of the set, so you would specify
2instead of3(examples are straight from the documentation).