How can I run a command five times using Ruby?
How can I run 开发者_如何学Goa command five times in a row?
For example:
5 * send_sms_to("xxx");
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') }
Here is an example using ranges:
(1..5).each { send_sms_to("xxx") }
Note: ranges constructed using ..
run from the beginning to the end inclusively.
精彩评论