Files
freeCodeCamp/guide/chinese/ruby/ruby-for-loop/index.md
Alexander Dervish 0191cb2eb4 Fix ruby for-loops examples (#30309)
* Fix ruby for-loops examples

* Fix alignment

* fix: corrected array.each do syntax

* fix: corrected array.each do syntax

* fix: correct array.each do syntax

* fix: corrected array.each do syntax

* fix: correct array.each do syntax

* fix: correct array.each do syntax
2019-03-28 17:27:39 -07:00

37 lines
995 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
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
```
for循环也可以利用Range的方式来执行1..10 代表 1~10 包含 101...10 代表 1~10 不包含 10:
```
for element in 1..10
puts element
end
```
在Ruby中有许多不同的方法可以执行for循环或循环另一个例子是
```
array.each do |element|
puts element
end
```
这将获得与上述for循环完全相同的结果但是它使用Array的内置方法更整洁更高效。
为了更进一步,我们可以通过以下方式编写上述循环:
```
array.each do |element| puts element end
```