ruby for 循环的语法 [英] Syntax for a for loop in ruby

查看:30
本文介绍了ruby for 循环的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Ruby 中执行这种类型的 for 循环?

How do I do this type of for loop in Ruby?

for(int i=0; i<array.length; i++) {

}

推荐答案

array.each do |element|
  element.do_stuff
end

for element in array do
  element.do_stuff
end

如果你需要索引,你可以使用这个:

If you need index, you can use this:

array.each_with_index do |element,index|
  element.do_stuff(index)
end

这篇关于ruby for 循环的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆