The count-taking method in Ruby is the most humane and flexible in the language I touch, which can be illustrated here for example:
First, print 7 times "Hello,world", as follows:
7.times{puts "Hello,world"} #在这里, puts function is equivalent to print+ "\ n"
Second, print 1 in turn. 9, there are more methods in Ruby, which are implemented in a simpler way, as follows:
(1..9). each{|i| Print I, ""}
Third, print 1. The value of 9 is smaller than 7, as follows:
1.upto (9) {|i| print I, "" If I < 7}
Cycle from 10 to 2 values, and then subtract 1 processing as follows:
10.downto (2) Do |i|print I, "" End
Five, 1-9 each number in turn, and then input the results, as follows:
A = 1..9b = a.collect{|i| i*2}p b[0..8]
Six, cycle from 3 to 19, and then add 3 processing, as follows:
3.step (19,3) do |i|print I, "" End
This article is from the "Fuqin Wine" blog, please make sure to keep this source http://yuhongchun.blog.51cto.com/1604432/1592632
User-friendly Ruby count value