028-86922220
建站资讯

网站建设资讯

为你提供网站建设行业资讯、网站优化知识、主机域名邮箱、网站开发常见问题等。

Ruby-迭代器

#迭代器
5.times { puts "Hello Woeld!"  }  #连续输出4次Hello World!

1.upto(10) { |i| puts "The count is #{i}" }   #从1数到10

10.downto(1) { |i| puts "The count is #{i}"  }    #从10到1

1.step(10, 2) { |i| puts "The count is #{i}"  }   #从1到10,步长为2

#each,使用each方法将集合内的对象逐个取出
name = ["MZD","JZM","HJT","XJP"]
name.each do |x|
  puts x
end

分享标题:Ruby-迭代器
当前链接:http://www.whjierui.cn/article/iidicp.html

其他资讯