How can I express the next for loops in a rubysh way?
for (r = 1; r < R; r++) {
for (i = 0; i < 4; i++) {
#do something
}
}
I want to express the above code with an elegant ruby syntax.
Thanks in advance.
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.
By default, the way I’d write that in Ruby is this:
But an even better way is to take advantage of the Range feature pointed out by @PedroNascimento in his answer, using three dots rather than two in order to leave the last item out of the Range:
That’s both nicer to look at and more clearly expresses the meaning of the code.