Is there a shorthand version of this loop in Ruby

1.6k Views Asked by At

I am writing a code that has lines limit of a method so I am trying to write the shortest possible version of this loop :

for i in (0..number)
  #lines of code
end

I was wondering if there is a way to do it somehow similar to:

{
  #lines of code
}*number

In general I am looking for the shortest possible way of writing something like this.

2

There are 2 best solutions below

0
On BEST ANSWER

some way to do a loop/iterator

 0.upto(number) { ... }

or

 number.upto(number) { ... }
0
On

Based on the second code block in your question, I'll conclude you do not need to reference the loop variable i within your loop. So the best solution in Ruby is:

(number+1).times {
    # code
}