Question

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.

Was it helpful?

Solution

some way to do a loop/iterator

 0.upto(number) { ... }

or

 number.upto(number) { ... }

OTHER TIPS

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top