-1

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.

user2128702
  • 2,059
  • 2
  • 29
  • 74
  • Can you be more specific, I am not able to understand... The one you are expecting, have you ever seen any other programming language, Then throw that code to us...to get the idea you are looking for in Ruby... – Arup Rakshit Dec 21 '13 at 18:58
  • 3
    5.times { puts "hello" } – Igor Kasyanchuk Dec 21 '13 at 19:03
  • @IgorKasyanchuk May be your catch is right!! Let's see what OP's response is.. – Arup Rakshit Dec 21 '13 at 19:05
  • Whether you need a counter inside a block: `(number+1).times { |i| puts i }` or `(0..number).each { |i| puts i }`. – Aleksei Matiushkin Dec 21 '13 at 19:10
  • This is a very poor question; you should have anticipated that it would not be understood. Moreover, you have not edited your question to clarify, even though Arup told you he (and presumably others) did not understand what you are asking. I suggest you edit the question (do not try to explain in comments) to give the complete method, together with an example that gives input and desired output. Maybe then you'll get some useful answers. – Cary Swoveland Dec 21 '13 at 20:13

2 Answers2

3

some way to do a loop/iterator

 0.upto(number) { ... }

or

 number.upto(number) { ... }
Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103
2

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
}
Darren Stone
  • 2,008
  • 13
  • 16