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
This commit is contained in:
committed by
Randell Dawson
parent
c606946c5c
commit
0191cb2eb4
@@ -8,9 +8,9 @@ Ruby for循环用于循环或迭代许多元素并执行每个元素的代码块
|
||||
|
||||
for循环只是循环或迭代元素的一个例子。下面是for循环的示例:
|
||||
```
|
||||
for element in array do
|
||||
puts element
|
||||
end
|
||||
for element in array do
|
||||
puts element
|
||||
end
|
||||
```
|
||||
|
||||
for循环也可以利用Range的方式来执行,1..10 代表 1~10 包含 10,1...10 代表 1~10 不包含 10:
|
||||
@@ -22,15 +22,15 @@ end
|
||||
|
||||
在Ruby中有许多不同的方法可以执行for循环或循环,另一个例子是:
|
||||
```
|
||||
element.each do |element|
|
||||
puts element
|
||||
end
|
||||
array.each do |element|
|
||||
puts element
|
||||
end
|
||||
```
|
||||
|
||||
这将获得与上述for循环完全相同的结果,但是它使用Array的内置方法更整洁,更高效。
|
||||
|
||||
为了更进一步,我们可以通过以下方式编写上述循环:
|
||||
```
|
||||
element.each do { |element| puts element }
|
||||
array.each do |element| puts element end
|
||||
|
||||
```
|
||||
|
Reference in New Issue
Block a user