How can I run a command five times using Ruby?

How can I run a team five times in a row?

For example:

5 * send_sms_to("xxx"); 
+45
ruby ruby-on-rails-3
Apr 15 2018-11-11T00:
source share
3 answers

To execute a command 5 times in a row, you can do

 5.times { send_sms_to("xxx") } 

See the documentation for times as well as times Ruby Essentials for more information.

+106
Apr 15 '11 at 2:30 p.m.
source share

You can use the times method of the Integer class:

 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 upto / downto :

 1.upto(5) { send_sms_to('xxx') } 
+38
Apr 15 '11 at 2:30 p.m.
source share

Here is an example of using ranges:

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

Note: ranges constructed using .. are run from start to finish, inclusive.

+5
Oct. 16 '12 at 9:15
source share



All Articles