[ACCEPTED]-How can I run a command five times using Ruby?-ruby-on-rails-3
Accepted answer
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
You can use the times
method of the class Integer
:
5.times do
send_sms_to('xxx')
end
or 1 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') }
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.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.