How can I run a command five times in a row?
For example:
5 * send_sms_to("xxx");
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.
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.
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.
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
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') }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With