ruby中的常用循环……

1、最常用的是each each_with_index

ss = [1,2,3,4,5]
ss.each do |s|
puts s
end

ss.each_with_index do |s,i|
puts ss[i]
end

 2、case(switch)语句

case x
  when "this"
    func_this_that()
  when "that"
    func_this_that()
  when "nothing"
    # something different
end
# Example 1
x = ["case4"]

case x
  when ["case1", "case2"]
    puts "first case"
  when ["case3", "case4", "case5"]
    puts "second case"
  when ["case6"]
    puts "third case"
  else
    puts "not matched"
end

# Returns: "second case"

# Example 2
x = [6]

case x
  when [1, 2]
    puts "first case"
  when [3, 4, 5]
    puts "second case"
  when [6]
    puts "third case"
  else
    puts "not matched"
end

# Returns: "third case"

 3、for 循环

for s in ss
puts s
end

 4、……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值