Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a command five times using Ruby?

How can I run a command five times in a row?

For example:

5 * send_sms_to("xxx"); 
like image 260
donald Avatar asked Apr 15 '11 14:04

donald


People also ask

What is times in Ruby?

The times function in Ruby returns all the numbers from 0 to one less than the number itself. It iterates the given block, passing in increasing values from 0 up to the limit. If no block is given, an Enumerator is returned instead. Parameter: The function takes the integer till which the numbers are returned.

What does loop return in Ruby?

The odd thing about the for loop is that the loop returns the collection of elements after it executes, whereas the earlier while loop examples return nil . Let's look at another example using an array instead of a range.

How do you stop an infinite loop in Ruby?

This means that the loop will run forever ( infinite loop ). To stop this, we can use break and we have used it. if a == "n" -> break : If a user enters n ( n for no ), then the loop will stop there. Any other statement of the loop will not be further executed.


2 Answers

To run a command 5 times in a row, you can do

5.times { send_sms_to("xxx") } 

For more info, see the times documentation and there's also the times section of Ruby Essentials

like image 154
Daniel DiPaolo Avatar answered Sep 23 '22 21:09

Daniel DiPaolo


You can use the times method of the class Integer:

5.times do     send_sms_to('xxx')  end 

or a for loop

for i in 1..5 do   send_sms_to('xxx') end 

or even a upto/downto:

1.upto(5) { send_sms_to('xxx') } 
like image 22
Andrei Andrushkevich Avatar answered Sep 26 '22 21:09

Andrei Andrushkevich