@@ -914,10 +914,9 @@ for procedures like `sin`. That's why they are called module functions.
914914
915915h2. Iterators
916916
917- Ruby's iterators differ a bit Java's or C++'s
918- iterator classes or 'Iterator' design patterns.
919- Precisely speaking those iterators are exterior iterators.
920- Ruby's iterators are called interior iterators.
917+ Ruby's iterators differ a bit from Java's or C++'s iterator classes
918+ or 'Iterator' design patterns. Precisely speaking those iterators
919+ are exterior iterators. Ruby's iterators are called interior iterators.
921920It's difficult to understand from the definition so
922921let's explain it with a concrete example.
923922
@@ -944,7 +943,7 @@ arr.each do |item|
944943end
945944</pre>
946945
947- From `each do` to `end` is all the call to an iterator method.
946+ Everything from `each do` to `end` is the call to an iterator method.
948947More precisely `each` is the iterator method and between
949948`do` and `end` is the iterator block.
950949The part between the vertical bars are the block parameters.
@@ -961,24 +960,24 @@ We can also think the other way round. The other parts except `print item`
961960are being cut out and inserted into the `each` method.
962961
963962<pre class="emlist">
964- <span class="ami"> i = 0
965- while i < arr.length</span>
966- print <span class="ami"> arr[i]</span>
967- <span class="ami"> i += 1
968- end</span>
963+ i = 0
964+ while i < arr.length
965+ print arr[i]
966+ i += 1
967+ end
969968
970- <span class="ami"> arr.each do |item|</span>
969+ arr.each do |item|
971970 print item
972- <span class="ami"> end</span>
971+ end
973972</pre>
974973
975974h3. Comparison with higher order functions
976975
977- What comes closest to iterators in C are function which receive function pointers,
976+ What comes closest in C to iterators are functions which receive function pointers,
978977or higher order functions. But there are two points in which iterators in Ruby
979- and higher functions in C differ.
978+ and higher order functions in C differ.
980979
981- Firstly, Ruby's iterators can only take one block. For instance we can't
980+ Firstly, Ruby iterators can only take one block. For instance we can't
982981do the following.
983982
984983<pre class="emlist">
@@ -990,7 +989,7 @@ end do |j|
990989end
991990</pre>
992991
993- Secondly, Ruby's block's can share local variables with the code outside.
992+ Secondly, Ruby's blocks can share local variables with the code outside.
994993
995994<pre class="emlist">
996995lvar = 'ok'
@@ -1004,11 +1003,8 @@ That's where iterators are convenient.
10041003But variables can only be shared with the outside. They cannot be shared
10051004with the inside of the iterator method ( e.g. `each`). Putting it intuitively,
10061005only the local variables can be seen, which are on the outside of the code.
1007- ただし変数を共有できるのはあくまでブロックの外側とだ。イテレータメソッ
1008- ド(例えば`each`)の中と共有するようなことはない。直感的に言うと、ソー
1009- スコードの外見がつながっている場所のローカル変数だけしか見えない。
10101006
1011- h3. Block local variables
1007+ h3. Block Local Variables
10121008
10131009Local variables which are assigned inside a block stay local to that block.
10141010They become block local variables. Let's check it out.
@@ -1020,10 +1016,9 @@ They become block local variables. Let's check it out.
10201016end
10211017</pre>
10221018
1023- とりあえずブロックを作るために、長さ1の配列に対して`each`で繰り返して
1024- おくことにする(ブロックパラメータは丸ごと省略してしまってもいいのだ)。
1025- そのブロックの中で変数`i`に初めて代入した……即ち宣言した。
1026- これでこの`i`はブロックローカルになる。
1019+ For the time being we apply each to an array of length 1. ( We can
1020+ leave out the block parameter.) The variable @i@ is first assigned
1021+ and declared inside the block. So @i@ becomes a block local variable.
10271022
10281023Block local means that it cannot be accessed from the outside.
10291024Let's test it.
@@ -1042,7 +1037,7 @@ for #<Object:0x40163a9c> (NameError)
10421037When we referenced a block local variable from outside the block
10431038an error occured. Without a doubt it stayed local to the block.
10441039
1045- Iterator can also be nested repeatedly. Each time
1040+ Iterators can also be nested repeatedly. Each time
10461041the new block creates another scope.
10471042
10481043<pre class="emlist">
@@ -1079,12 +1074,11 @@ variables `i` are different.
10791074}
10801075</pre>
10811076
1082- 内側のブロックの中にいる間はあたかも内側の`i`が外側の`i`を覆い隠して
1083- いるかのようだ。その「覆い隠すこと」をshadowingという。shadowは影だか
1084- ら、自分の影の中に入れて(その結果隠して)しまう、という意味である。
1077+ Inside the block the @i@ inside overshadows the @i@ outside.
1078+ That's why it's called shadowing.
10851079
1086- ではshadowingをしないRubyのブロックローカル変数の場合はどうなるか。
1087- 次の例を見てほしい。
1080+ But what happens in Ruby where there's no shadowing.
1081+ Let's look at this example.
10881082
10891083<pre class="emlist">
10901084i = 0
@@ -1093,14 +1087,14 @@ p i # 0
10931087 i = 1
10941088 p i # 1
10951089end
1096- p i # 1(変更されたまま)
1090+ p i # 1 the change is preserved
10971091</pre>
10981092
1099- ブロックの内側で`i`に代入しても外側に同じ名前があればそれを使う。
1100- だから内側で`i`に代入すれば外側の`i`の値が変わる。
1101- この点に関しては何度も何度も「間違いやすいからshadowingしてくれ」
1102- という文句が出てそのたびにフレーム寸前になるのだが、
1103- いまだに結論は出ていない。
1093+ When we assign @i@ inside the block and if there is a variable @i@
1094+ that same variable will be used. Hence if we assign to @i@ inside
1095+ the value for @i@ on the outside changes. On this point there
1096+ came many complains: "This is error prone. Please do shadowing."
1097+ Each time there's flaming but till now no conclusion was reached.
11041098
11051099h3. The syntax of iterators
11061100
@@ -1167,10 +1161,10 @@ end
11671161end
11681162</pre>
11691163
1170- ` yield`がブロックの呼び出しだ。その時点で渡されたブロックに制御が移り、
1171- ブロックの実行が終われば同じ場所に戻ってくる。特殊な関数呼び出しみたい
1172- なものだと思えばいい。もし現在のメソッドがブロック付き呼び出し(イテレー
1173- タ)でなければ実行時エラーになる。
1164+ @ yield@ calls the block. At this point control is passed to the block,
1165+ when the execution of the block finishes it returns back to the same
1166+ location. Think about it like calling a special function. When the
1167+ present method does not have a block a runtime error will occur.
11741168
11751169<pre class="screen">
11761170% ruby -e '[0,1,2].each'
@@ -1180,74 +1174,76 @@ end
11801174
11811175h3. `Proc`
11821176
1183- イテレータはコードを切り取って引数として渡すようなものだ、と話した。
1184- だが実はもっと直接的に、コードをオブジェクトにして持ち運ぶことも
1185- できる。
1177+ I said, that iterators are like cut out code which is passed as an
1178+ argument. But we can even more directly make code to an object
1179+ and carry it around.
11861180
11871181<pre class="emlist">
11881182twice = Proc.new {|n| n * 2 }
1189- p twice.call(9) # 18と表示される
1183+ p twice.call(9) # 18 will be printed
11901184</pre>
11911185
1192- ようするに関数みたいなものだ。`new`を使って生成していることからも
1193- 想像できるとおり、`Proc.new`の返り値は`Proc`クラスのインスタンスである。
1186+ In short, it is like a function. It can be created with @new@ and
1187+ as might be expected, the return value of @Proc.new@ is an instance
1188+ of the @Proc@ class.
11941189
1195- `Proc.new`の見ためは明らかにイテレータだし、実際その通りである。
1196- これは普通のイテレータだ。ただちょっと`Proc.new`の内部でゴソゴソやって、
1197- イテレータブロックをオブジェクト化するような仕掛けを動かしているに
1198- すぎない。
1190+ @Proc.new@ looks surely like an iterator and it is indeed so.
1191+ It is an ordinary iterator. There's only some mechanism inside @Proc.new@
1192+ which turns an iterator block into an object.
11991193
1200- ちなみに`Proc.new`と同じ効果の` lambda`という関数風メソッドも用意されて
1201- いる。好みで選んでもらえばよい。
1194+ Besides there is a function style method @ lambda@ provided which
1195+ has the same effect as @Proc.new@. Choose whatever suits you.
12021196
12031197<pre class="emlist">
12041198twice = lambda {|n| n * 2 }
12051199</pre>
12061200
1207- h4. イテレータと `Proc`
1201+ h4. Iterators and `Proc`
12081202
1209- なぜ突然`Proc`の話を始めたかと言うと、イテレータと`Proc`にはとても深い関係
1210- があるからだ。実のところイテレータブロックと`Proc`オブジェクトとは「同じ
1211- もの」なのである。だから相互に変換可能だ。
1203+ Why did we start talking all of a sudden about @Proc@? Because there
1204+ is a deep relationship between iterators and @Proc@.
1205+ In fact iterators and @Proc@ objects are quite the same thing.
1206+ That's why one can be transformed into the other.
12121207
1213- まずイテレータブロックを` Proc`オブジェクトにするには
1214- パラメータ名の前に`&`を付ければよい。
1208+ First, to turn an iterator block into a @ Proc@ object
1209+ one has to put an @&@ in front of the parameter name.
12151210
12161211<pre class="emlist">
12171212def print_block( &block )
12181213 p block
12191214end
12201215
1221- print_block() do end # # <Proc:0x40155884>のように表示される
1222- print_block() # ブロックなしなのでnilと表示される
1216+ print_block() do end # Shows something like <Proc:0x40155884>
1217+ print_block() # Without a block nil is printed
12231218</pre>
12241219
1225- 引数名の前に`&`を付けると、ブロックが`Proc`オブジェクトに変換されその変数
1226- に代入される。メソッドがイテレータでないとき
1227- (ブロック付き呼び出しではないとき)には` nil`が入る。
1220+ With an @&@ in front of the argument name, the block is transformed to
1221+ a @Proc@ object and assigned to the variable. If the method is not an
1222+ iterator (there's no block attached) @ nil@ is assigned.
12281223
1229- そしてその逆、`Proc`をイテレータブロックとして渡すにもやはり`&`を使う。
1224+ And in the other direction, if we want to pass a @Proc@ to an iterator
1225+ we also use @&@.
12301226
12311227<pre class="emlist">
12321228block = Proc.new {|i| p i }
12331229[0,1,2].each(&block)
12341230</pre>
12351231
1236- このコードは以下と全く同じ意味を持つ。
1232+ This code means exactly the same as the code below.
12371233
12381234<pre class="emlist">
12391235[0,1,2].each {|i| p i }
12401236</pre>
12411237
1242- この両方を組み合わせると、イテレータブロックを
1243- 他所のメソッドに丸投げすることもできるようになる。
1238+ If we combine these two, we can delegate an iterator
1239+ block to a method somewhere else.
12441240
12451241<pre class="emlist">
12461242def each_item( &block )
12471243 [0,1,2].each(&block)
12481244end
12491245
1250- each_item do |i| # [0,1,2].each do |i|と同じこと
1246+ each_item do |i| # same as [0,1,2].each do |i|
12511247 p i
12521248end
12531249</pre>
0 commit comments