3.times { print "Hi!" } #Hi!Hi!Hi!
1.upto(9) {|i| print i if i<7 } #123456
9.downto(1){|i| print i if i<7 } #654321
(1..9).each {|i| print i if i<7} #123456
0.step(11,3) {|i| print i } #0369
#--------------------------
while 条件 do
end
for i in [0...100]
end
[b]方法select/detect/collect的区别?
select得到匹配条件的结果集合
detect得到匹配条件的第一个值
collect 通过迭代组合新array[/b]
[1,2,3,4,5,6,7,8,9,10].select do |i|
i > 3 && i < 8
end
#返回 [4,5,6,7]
[1,2,3,4,5,6,7,8,9,10].detect do |i|
i > 3
end
#返回 4
[1,2,3,4,5,6,7,8,9,10].collect do |i|
i += 100
end
#返回 [101,102,103,104,105,106,107,108,109,110]