开发者

How can I run a command five times using Ruby?

开发者 https://www.devze.com 2023-02-26 02:27 出处:网络
How can I run 开发者_如何学Goa command five times in a row? For example: 5 * send_sms_to(\"xxx\");

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消