freeCodeCamp/guide/chinese/ruby/ruby-for-loop/index.md

29 lines
843 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Ruby For Loops
localeTitle: Ruby For循环
---
## Ruby For循环
Ruby for循环用于循环或迭代许多元素并执行每个元素的代码块。 For循环通常用在数组上。请参阅[Ruby Arrays](https://github.com/freeCodeCamp/guides/blob/master/src/pages/ruby/ruby-arrays/index.md)部分。
for循环只是循环或迭代元素的一个例子。下面是for循环的示例
```
for element in array do
puts element
end
```
在Ruby中有许多不同的方法可以执行for循环或循环另一个例子是
```
element.each do |element|
puts element
end
```
这将获得与上述for循环完全相同的结果但是它使用Array的内置方法更整洁更高效。
为了更进一步,我们可以通过以下方式编写上述循环:
```
element.each do { |element| puts element }
```