Ruby 基础教程1-6

1.循环实现方法

  • 循环语句 (while;for; loop,until)
  • 循环方法(times,each)

2.for

for 变量 in 对象

主体

end

3.while

while 条件

end

4.until与while相反 条件不成立才进入循环体

until 条件

end

5.循环控制

  • break
  • next
  • redo 重复执行当前循环,循环变量不变

6.选择

  • times 确定循环次数
  • for 从对象中取元素
  • each 从对象中取元素
  • while 自由指定循环条件
  • until 当while条件变的难懂的时候
  • loop 不限制循环次数

arr=[1,2,3,"a","b","c"]

for item in arr

puts item

end

for i in 1..3

puts i

end

arr.each {|item| puts item}