循环红宝石的简单计数器

che*_*ell 16 ruby

对于红宝石,我可以做一个计数器,或者我必须做以下事情

count = 0
4.times do
puts "this is the count #{count}"
count = count+1
Run Code Online (Sandbox Code Playgroud)

sep*_*p2k 30

是的,times产生一个柜台:

4.times do |count|
  puts "this is the count #{count}"
end
Run Code Online (Sandbox Code Playgroud)

  • `(1..4).each {| count | 把"这是计数#{count}"}`或`for count in 1..4 puts"这是计数#{count}"end` (2认同)
  • @chell:不,因为你可以将起始值添加到由`times`产生的计数中,或者使用类似`4.upto(7)do | i | ...结束`迭代4到7号. (2认同)