• ruby各种循环输出数组元素


    array = (1..10).to_a
    # 方法1
    length = array.length  
    length.times do |t|  
     print "#{array[t]} "  
    end  
    puts "
    "
    
    # 方法2
    length = array.length-1  
    for i in 0..length do  
      print "#{array[i]} "  
    end
    puts "
    "
    
    # 方法3
    for i in array do  
      print "#{i} "  
    end 
    puts "
    "
    
    # 方法4
    array.each{|x| print x," "}
    puts "
    "
    
    # 方法5
    length = array.length  
    i = 0  
    while i< length do  
      print "#{array[i]} "  
      i = i+1  
    end 
    puts "
    "
    
    # 方法6
    length = array.length  
    i = 0  
    until i==length do  
      print "#{array[i]} "  
      i += 1  
    end  
    puts "
    "
    
    # 方法7
    array.each_index do |i|  
        print "#{array[i]} "  
    end  
    puts "
    "
    
    # 方法8
    length = array.length - 1  
    0.upto(length) do |i|
      print "#{array[i]} "  
    end
    puts "
    "
    
    # 方法9
    length = array.length - 1  
    i = 0  
    loop do  
      print "#{array[i]} "  
      i += 1  
      break if i > length #也可以用break unless i <= length   
    end  
  • 相关阅读:
    【git】之常用命令
    VueJs
    如何做技术选型
    用户登录验证注意事项
    npm xss
    express-session
    ejs、jade和dust
    XMLHttpRequest.responseType
    JS对象字面量
    安装Angulr CLI
  • 原文地址:https://www.cnblogs.com/cyttina/p/3245687.html
Copyright © 2020-2023  润新知