mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-13 18:24:39 +02:00
Tidy up wording
This commit is contained in:
@@ -187,14 +187,14 @@ end
|
|||||||
#=> iteration 4
|
#=> iteration 4
|
||||||
#=> iteration 5
|
#=> iteration 5
|
||||||
|
|
||||||
# HOWEVER
|
# HOWEVER, No-one uses for loops.
|
||||||
# No-one uses for loops
|
# Instead you should use the "each" method and pass it a block.
|
||||||
# Under the hood for loops use the each method which takes a "block".
|
# A block is a bunch of code that you can pass to a method like "each".
|
||||||
# A block is a bunch of code that you can pass to a method like each.
|
|
||||||
# It is analogous to lambdas, anonymous functions or closures in other programming languages.
|
# It is analogous to lambdas, anonymous functions or closures in other programming languages.
|
||||||
|
#
|
||||||
# The each method runs the block multiple times passing a counter.
|
# The "each" method of a range runs the block once for each element of the range.
|
||||||
# You can iterate over a range like this:
|
# The block is passed a counter as a parameter.
|
||||||
|
# Calling the "each" method with a block looks like this:
|
||||||
|
|
||||||
(1..5).each do |counter|
|
(1..5).each do |counter|
|
||||||
puts "iteration #{counter}"
|
puts "iteration #{counter}"
|
||||||
@@ -208,7 +208,7 @@ end
|
|||||||
# You can also surround blocks in curly brackets:
|
# You can also surround blocks in curly brackets:
|
||||||
(1..5).each {|counter| puts "iteration #{counter}"}
|
(1..5).each {|counter| puts "iteration #{counter}"}
|
||||||
|
|
||||||
# You can also iterate over the contents of data structures using each.
|
# The contents of data structures can also be iterated using each.
|
||||||
array.each do |element|
|
array.each do |element|
|
||||||
puts "#{element} is part of the array"
|
puts "#{element} is part of the array"
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user