[ACCEPTED]-How can I run a command five times using Ruby?-ruby-on-rails-3

Accepted answer
Score: 138

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

5.times { send_sms_to("xxx") }

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

Score: 46

You can use the times method of the class Integer:

5.times do 

or 1 a for loop

for i in 1..5 do

or even a upto/downto:

1.upto(5) { send_sms_to('xxx') }
Score: 6

Here is an example using ranges:

(1..5).each { send_sms_to("xxx") }

Note: ranges constructed 2 using .. run from the beginning to the end 1 inclusively.

More Related questions