21 天Ruby-------- loop


class ForLoop

def callFor
for i in 1..3
print "index = #{i}\n"
end
end

def callWhile
a = 1
a *= 2 while a < 10
puts a

a -= 2 until a < 0
puts a
end

def callWhile2
a = 1
a += 1 while a < 5
puts a
# print "index = #{a}\n"
end

def time
3.times do
print "Ho! "
end
end

def upto
1.upto(3) do |x|
puts x
end
end

def step
0.step(10, 2) do |x|
puts x
end
end

def each
[1, 2, 3, 4, 5].each do |x|
puts x
end
end

def callFor2 # seems similar as each
for n in [5, 4, 3, 2, 1]
puts n
end
end

def callLoop
i = 0
loop do
i += 1
puts i
next if not i > 3
puts "i > 3"
redo if i >= 6 and i <= 9
puts "i < 6 or i > 9"
break if i == 10
end
end

end

instance = ForLoop.new
instance.callFor
print "----------\n"
instance.callWhile2
print "----------\n"
instance.time
print "\n----------\n"
instance.upto
print "----------\n"
instance.step
print "----------\n"
instance.each
print "----------\n"
instance.callFor2
print "----------\n"
instance.callLoop

# output
#index = 1
#index = 2
#index = 3
#----------
#5
#----------
#Ho! Ho! Ho!
#----------
#1
#2
#3
#----------
#0
#2
#4
#6
#8
#10
#----------
#1
#2
#3
#4
#5
#----------
#5
#4
#3
#2
#1
#----------
#1
#2
#3
#4
#i > 3
#i < 6 or i > 9
#5
#i > 3
#i < 6 or i > 9
#6
#i > 3
#7
#i > 3
#8
#i > 3
#9
#i > 3
#10
#i > 3
#i < 6 or i > 9



ruby loop变现形式真多
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值