@@ -272,192 +272,175 @@ p(=emlist).
272272result = if cond then process(val) else nil end
273273</pre>
274274
275- 大雑把に言うと、関数やメソッドの引数にできるものは式だと思っていい。
275+ Roughly, you can assume any element which can be fed as an argument for a function
276+ or a method is a formula.
276277
278+ Of course, there are other languages whose syntactic elements are mostly a formula.
279+ Namely, Lisp is the best example. For this characteristic, many people seem to
280+ regard Ruby is similar to Lisp.
277281
278- もちろん「ほとんどの文法要素が式」という言語は他にもいろいろある。例えば
279- Lispはその最たるものだ。このあたりの特徴からなんとなく「RubyはLispに似
280- てる」と感じる人が多いようである。
282+ h4. Iterators
281283
282- h4. イテレータ
284+ Ruby has iterators. What is an iterator? Before getting into iterators, I should
285+ mention the necessity of using an alternative term, because the word "iterator" is
286+ disliked recently. However, I don't have a good alternation. So let us keep calling
287+ it "iterator" for the time being.
283288
284- Rubyにはイテレータがある。イテレータとは何か。いやその前にイテレータと
285- いう言葉は最近嫌われているので別の言葉を使うべきかもしれない。だがい
286- い言葉を思いつかないので当面イテレータと呼ぶことにする。
289+ Well again, what is an iterator? If you are already familiar with a high level
290+ function which is similar, you can assume it for now. C-language example is the way
291+ you feed function pointer as an argument. C++ example is a way to envelope the
292+ operation part of STL's @Iterator@ into a method. If you are familiar with sh and
293+ Perl, you can imagine it like a custom defined @for@ statement.
287294
295+ Yet, the above are merely examples of "similar" concepts. All of them are similar,
296+ but they are not identical to Ruby's iterator. I will expand the precise story
297+ when it's a good time later.
288298
289- それでイテレータとは何か。高階の関数を知っているなら、とりあえずはそれ
290- と似たようなものだと思っておけばいい。Cで言えば関数ポインタを引数
291- に渡すやつである。C++で言えばSTLにある@Iterator@の操作部分までをメソッド
292- に封入したものである。shやPerlを知っているのなら、独自に定義できる
293- @for@文みたいなもんだと思って見てみるといい。
299+ h4. Written in C-language
294300
301+ Being written in C-language is not distinctive these days, but it's still a notable
302+ characteristic for sure. Unlike being written in Haskell or PL/I, it should possibly
303+ be readable for a wide range of people generally. (I want you to confirm it in the
304+ later in this book.)
295305
296- もっともあくまでここに挙げたのは全て「似たようなもの」であって、どれも
297- Rubyのイテレータと似てはいるが、同じでは、全くない。いずれその時が来た
298- らもう少し厳密な話をしよう。
306+ Well, I just said it's in C-language, but the actual language version which ruby is
307+ targetting is basically K&R C. Until a little while ago, there were a decent number
308+ of - not plenty though - K&R-only-environment. Considering the trend of ANSI C being
309+ a popular environment that seems to pass most of programs, there appears to be no
310+ problem to port to ANSI C. However, the K&R style code is still maintained, with the
311+ author Matsumoto's preference being one aspect.
299312
300- h4. C言語で書いてある
313+ For this reason, the function definition is all in K&R style, and the prototype
314+ declarations are not strictly written. Looking at the mailing list, I can see a lot
315+ of topics in which @gcc@ with @-Wall@ option outputs a huge dump of errors, and C++
316+ compiler warns prototype mismatch that fails to finish compiling.
301317
302- Cで書いたプログラムなどいまどき珍しくもないが、特徴であることは間違い
303- ない。少なくともHaskellやPL/Iで書いてあるわけではないので一般人にも
304- 読める可能性が高い(本当にそうかどうかはこれから自分で確かめてほしい)。
318+ h4. Extension library
305319
320+ Ruby library can be written in C. It can be loaded at execution without recompiling Ruby.
321+ This type of library is called "Ruby extension library" or just "Extension library".
306322
307- それからC言語と言っても@ruby@が対象としているのは基本的にK&R Cだ。
308- 少し前まではK&R onlyの環境が、たくさんとは言わないが、それなりにあったからだ。
309- しかしさすがに最近はANSI Cが通らない環境はなくなってきており技術的には
310- ANSI Cに移っても特に問題はない。だが作者のまつもとさん個人の趣味もあっ
311- てまだK&Rスタイルを通している。
312-
313-
314- そんなわけで関数定義は全てK&Rスタイルだし、プロトタイプ宣言もあまり真面
315- 目に書かれていない。@gcc@でうっかり@-Wall@を付けると大量に警告が出て
316- くるとか、
317- C++コンパイラでコンパイルするとプロトタイプが合わないと怒られてコンパ
318- イルできない......なんて話がポロポロとメーリングリストに流れている。
319-
320- h4. 拡張ライブラリ
321-
322- RubyのライブラリをCで書くことができ、Rubyを再コンパイルすることなく
323- 実行時にロードできる。このようなライブラリを「Ruby拡張ライブラリ」
324- または単に「拡張ライブラリ」と言う。
325-
326-
327- 単にCで書けるだけでなくRubyレベルとCレベルでのコードの表現の差が小さい
328- のも大きな特徴である。Rubyで使える命令はほとんどそのままCでも使うこと
329- ができる。例えば以下のように。
323+ In addition to the fact that's written in C, one of the important feature is that Ruby
324+ level and C level code differences are very small. Most commands available in Ruby are
325+ also available in C. See following example.
330326
331327p(=emlist).
332- # メソッド呼び出し
328+ # Method call
333329obj.method(arg) # Ruby
334330rb_funcall(obj, rb_intern("method"), 1, arg); # C
335- # ブロック呼び出し
331+
332+ # Block call
336333yield arg # Ruby
337334rb_yield(arg); # C
338- # 例外送出
335+
336+ # Raising exception
339337raise ArgumentError, 'wrong number of arguments' # Ruby
340338rb_raise(rb_eArgError, "wrong number of arguments"); # C
341- # オブジェクトの生成
339+
340+ # Generating an object
342341arr = Array.new # Ruby
343342VALUE arr = rb_ary_new(); # C
344343</pre>
345344
346- 拡張ライブラリを書くうえでは非常に楽をできていいし、現実に
347- このことが代えがたい @ruby@の長所にもなっている。しかしそのぶん
348- @ruby@の実装にとっては非常に重い足枷となっており、随所にその
349- 影響を見ることができる。特にGCやスレッドへの影響は顕著である。
345+ It's good because it provides easiness in composing an extension library, and actually
346+ it makes an indispensable prominence of @ruby@. However, it's also a burden for @ruby@
347+ implementation. You can see the affects of it in many places. The affects to GC and
348+ thread-processing is eminent.
350349
351- h4. スレッド
350+ h4. Thread
352351
353- Rubyにはスレッドがある。さすがに最近はスレッドを知らない人はほとんどい
354- ないと思うのでスレッド自体に関する説明は省略する。以下はもう少し細かい
355- 話だ。
352+ Ruby is equipped with thread. Assuming a very few people knowing none about thread these
353+ days, I will omit an explanation about the thread itself. I will start a story in detail.
356354
355+ The thread level of @ruby@ belongs to the user level of origin. The characteristic of
356+ this implementation is a very high portability in both specification and implementation.
357+ Surprisingly a MS-DOS can run the thread. Furthermore, you can expect the same response
358+ in any environment. Many people mention that this point is the best feature of @ruby@.
357359
358- @ruby@のスレッドはオリジナルのユーザレベルスレッドである。この実装の
359- 特徴は、仕様と実装、両方の移植性が非常に高いことである。なにしろDOS上で
360- さえスレッドが動き、どこでも同じ挙動で使えるのだ。この点を@ruby@の最大の
361- 長所として挙げる人も多い。
360+ However, as a trade off for such an extremeness of portability, @ruby@ abandons the speed.
361+ It's, say, probably the slowest of all user-level thread implementations in this world.
362+ The trend of @ruby@ implementation may be seen here the most clearly.
362363
364+ h2. Technique to read source code
363365
364- しかし@ruby@スレッドは凄まじい移植性を実現した反面で速度をおもいきり犠牲
365- にしている。どのくらい遅いかというと、世の中に数あるユーザレベルスレッ
366- ドの実装の中でも一番遅いのではないか、というくらい遅い。これほど@ruby@の
367- 実装の傾向を明確に表しているところもないだろう。
366+ Well. After an introduction of @ruby@, we are about to start reading source code. But wait.
368367
369- h2. ソースコードを読む技術
368+ Any programmer has to read a source code somewhere, but I guess there are not many occasions
369+ that someone teaches you the concrete ways how to read. Why? Is it because writing a program
370+ naturally assumes reading a program?
370371
371- さて。@ruby@の紹介も終わっていよいよソースコード読みに入ろうか、というと
372- ころだが、ちょっと待ってほしい。
372+ I don't think so. It's not easy to actually read a code written by other people.
373+ In the same way as writing programs, there must be techniques and theories in reading programs.
374+ And they are necessary. Therefore, before starting to ready @ruby@, I'd like to expand a general
375+ summary of an approach you need to take in reading a source code.
373376
377+ h3. Principles
374378
375- ソースコードを読む、というのはプログラマならば誰しもやらなければいけな
376- いことだが、その具体的な方法を教えてもらえることはあまりないのではない
377- だろうか。どうしてだろう。プログラムが書けるなら読むのも当然できるとい
378- うのだろうか。
379+ At first, I mention the principle.
379380
381+ h4. Decide a goal
380382
381- しかし筆者には人の書いたプログラムを読むことがそんなに簡単なことだとは
382- 思えない。プログラムを書くのと同じくらい、読むことにも技術や定石がある
383- はずだし、必要だと考える。そこで@ruby@を読んでいく前にもう少し一般的に、
384- ソースコードを読むにはどういう考えかたをすればいいのか、整理することに
385- しよう。
386383
387- h3. 原則
388-
389- まずは原則について触れる。
390-
391- h4. 目的の決定
392384bq.
393- 「ソースコードを読むための極意」は『目的をもって読む』ことです。
394-
395-
396- これはRuby作者のまつもとさんの言だ。なるほど、この言葉には非常にうなず
397- けるものがある。「カーネルくらいは読んどかなきゃいかんかなあ」と思って
398- ソースコードを展開したり解説本を買ったりしてはみたものの、いったいどう
399- していいのかわからないまま放ってしまった、という経験のある人は多いので
400- はないだろうか。その一方で、「このツールのどこかにバグがある、とにかく
401- これを速攻で直して動かさないと納期に間に合わない」......というときには他
402- 人のプログラムだろうとなんだろうと瞬く間に直せてしまうこともあるのでは
403- ないだろうか。
404-
405-
406- この二つのケースで違うのは、意識の持ちかたである。自分が何を知ろうと
407- しているのかわからなければ「わかる」ことはありえない。だからまず自分が
408- 何を知りたいのか、それを明確に言葉にすることが全ての第一歩である。
409-
410-
411- だがこれだけではもちろん「技術」たりえない。「技術」とは、意識すれば誰に
412- でもできるものでなければならないからだ。続いて、この第一歩から最終的に
413- 目的を達成するところまで敷衍する方法について延べる。
385+ An important key to reading the source code is to set a concrete goal.
414386
415- h4. 目的の具体化
416387
417- いま「@ruby@全部を理解する」を最終目標に決めたとしよう。これでも「目的を
418- 決めた」とは言えそうだが、しかし実際にソースコードを読む役に立たないこ
419- とは明らかである。具体的な作業には何にもつながっていないからだ。従って
420- まずはこの曖昧な目標を具体的なところまで引きずり下ろさなければならない。
388+ This is a word by the author of Ruby, Matsumoto. Indeed, his word is very convincing for me.
389+ When the motivation is a spontaneous idea "May be I should read a kernel, at least...",
390+ you would get source code expanded or explanatory books ready on the desk. But not knowing
391+ what to do, the studies are to be left untouched. Haven't you? On the other hand, when you
392+ have in mind "I'm sure there is a bug somewhere in this tool. I need to quickly fix it and
393+ make it work. Otherwise I will not be able to make the deadline...", you will probably be
394+ able to fix the code in a blink, even if it's written by someone else. Haven't you?
421395
396+ The difference in these two cases is motivation you have. In order to know something,
397+ you at least have to know what you want to know. Therefore, the first step of all is
398+ to figure out what you want to know in explicit words.
422399
423- どうすればいいだろうか。まず第一に、そのプログラムを書いた人間になった
424- つもりで考えてみることだ。そのときにはプログラムを作るときの知識が流用
425- できる。例えば伝統的な「構造化」プログラムを読むとしたら、こちらも
426- 構造化プログラムの手法に則って考えるようにする。即ち目的を徐々に徐々に分割
427- していく。あるいはGUIプログラムのようにイベントループに入ってグルグル
428- するものならば、とりあえず適当にイベントループを眺めてからイベントハン
429- ドラの役割を調べてみる。あるいはMVC(Model View Controler)のMをまず調
430- べてみる。
400+ However, of course this is not all needed to make it your own "technique".
401+ Because "technique" needs to be a common method that anybody can make use of it by following it.
402+ In the following section, I will explain how to bring the first step into the landing place
403+ where you achieve the goal finally.
431404
405+ h4. Visualising the goal
432406
433- 第二に解析の手法を意識することだ。誰しも自分なりの解析方法というのはそ
434- れなりに持っていると思うが、それは経験と勘に頼って行われていることが多
435- い。どうしたらうまくソースコードを読めるのか、そのこと自体を考え、意識
436- することが非常に重要である。
407+ Now let us suppose that our final goal is set "Understand all about @ruby@". This is certainly
408+ considered as "one set goal", but apparently it will not be useful for reading the source code
409+ actually. It will not be a trigger of any concrete action. Therefore, your first job will be to
410+ drag down the vague goal to the level of a concrete thing.
437411
412+ Then, what will the method be? Firstly, it's good if you pretend that you are the composer of
413+ the program. You can utilize your knowledge in writing a program, in this case. For example,
414+ when you are reading a traditional "structured" programming by somebody, you will analyze it
415+ hiring the strategies of structured programming too. That is, you will divide the target into
416+ pieces, little by little. For another example, suppose you are reading a GUI program that walks
417+ around in an event loop, you can just start looking at the event loop before studying the roles
418+ of the event handlers. In another case, you will look up what "M" is for in MVC (Model View Controller).
438419
439- ではそのような手法にはどんなものがあるだろうか。それを次に説明する。
420+ Secondly, keep in mind the methods of analysis. Everybody might have certain analysis methods,
421+ but they are often done based on experience and relying inspiration. It's crucially important
422+ to give it to the notion how to read source code well.
440423
441- h3. 解析の手法
424+ Well, what are such methods like? I will explain it in the next section.
442425
443- ソースコードを読む手法は大雑把に言って静的な手法と動的な手法の二つに分
444- 類できる。静的な手法とはプログラムを動かさずソースコードを読んだり解析
445- したりすること。動的な手法とはデバッガなどのツールを使って実際の動きを
446- 見ることだ。
426+ h3. Analysis methods
447427
428+ The methods to read source code can be roughly divided into two; one is a static method and
429+ the other is dynamic method. Static method is to read and analyze the source code without
430+ running the program. Dynamic method is to watch the actual behavior using tools like a debugger.
448431
449- プログラムを調査するときはまず動的な解析から始めたほうがよい。なぜなら
450- それは「事実」だからだ。静的な解析では現実にプログラムを動かしていない
451- のでその結果は多かれ少なかれ「予想」になってしまう。真実を知りたいのな
452- らばまず事実から始めるべきなのだ。
432+ It's better to start studying a program by dynamic analysis. That is because what you can see
433+ there is the "fact". The results from static analysis, due to the fact of not running the program
434+ actually, may well be "prediction" to a greater or lesser extent. If you want to know the truth,
435+ you should start from watching the fact.
453436
437+ Of course, you don't know whether the results of dynamic analysis are the fact really.
438+ The debugger could run with a bug, or the CPU may not be working properly due to overheat.
439+ The conditions of your configuration could be wrong. However, the results of static analysis
440+ should at least be closer to the fact than dynamic analysis.
454441
455- もちろん動的な解析の結果が本当に事実であるかどうかはわからない。デバッガがバ
456- グっているかもしれないし、CPUが熱暴走しているかもしれない。自分が設定
457- した条件が間違っているかもしれない。しかし少なくとも静的解析よりは動的
458- な解析の結果のほうが事実に近いはずである。
442+ h3. Dynamic analysis
459443
460- h3. 動的な解析
461444h4. 対象プログラムを使う
462445
463446これがなければ始まらない。そもそもそのプログラムがどういうものなのか、
0 commit comments