@@ -31,8 +31,8 @@ We will talk about local variables in the third part of the book.
3131
3232h3. API for variables
3333
34- The object of this chapter's analysis is `variable.c`. Let's first
35- look at the available API .
34+ The object of this chapter's analysis is `variable.c`. Let me first
35+ introduce the APIs which would be the entry points .
3636
3737<pre class="emlist">
3838VALUE rb_iv_get(VALUE obj, char *name)
@@ -41,9 +41,9 @@ VALUE rb_iv_set(VALUE obj, char *name, VALUE val)
4141VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
4242</pre>
4343
44- We've already spoken about those functions, but must mention them again
45- as they are in `variable.c`. They are of course used for accessing instance
46- variables .
44+ These are the APIs to access instance variables which have already been
45+ described. They are shown here again because their definitions are in
46+ `variable.c` .
4747
4848<pre class="emlist">
4949VALUE rb_cv_get(VALUE klass, char *name)
@@ -83,17 +83,18 @@ These last functions are for accessing global variables. They are a
8383little different from the others due to the use of `struct
8484global_entry`. We'll explain this while describing the implementation.
8585
86- h3. Important points
86+ h3. Points of this chapter
8787
88- The most important topic of this chapter is "Where and how are variables
88+ The most important point when talking about variables is "Where and how are variables
8989stored?", in other words: data structures.
9090
9191The second most important matter is how we search for the values. The scopes
9292of Ruby variables and constants are quite complicated because
9393variables and constants are sometimes inherited, sometimes looked for
9494outside of the local scope... To have a better understanding, you
95- should first try to guess from the behavior how it could be
96- implemented, then compare that with what is really done.
95+ should think by comparing the implementation with the specification,
96+ like "It behaves like this in this situation so its implementation
97+ couldn't be other then this!"
9798
9899h2. Class variables
99100
@@ -150,8 +151,9 @@ function for raising an exception, so it can be ignored for the same
150151reasons. In `ruby`, you can assume that all functions ending with
151152`_error` raise an exception.
152153
153- After removing all this, we can see that while following the `klass`'s
154- superclass chain we only search in `iv_tbl`. At this point you should
154+ After removing all this, we can see that it is just following the `klass`'s
155+ superclass chain one by one and searching in each `iv_tbl`.
156+ ... At this point, I'd like you to
155157say "What? `iv_tbl` is the instance variables table, isn't it?" As a
156158matter of fact, class variables are stored in the instance variable
157159table.
@@ -256,8 +258,8 @@ really work.
256258
257259h4. `rb_const_get()`
258260
259- We'll now look at `rconst_get ()`, the function to read a
260- constant. This functions returns the constant referred to by `id` from the class
261+ We'll now look at `rb_const_get ()`, the function to read a
262+ constant. This function returns the constant referred to by `id` from the class
261263`klass`.
262264
263265▼ `rb_const_get()`
@@ -392,8 +394,9 @@ top-level. Built-in classes like `String` or `Array` have for example
392394an entry in it. That's why we should not forget to search in this
393395table when looking for top-level constants.
394396
395- The next block is related to autoloading. This allows us to automatically
396- load a library when accessing a top-level constant for the first
397+ The next block is related to autoloading.
398+ It is designed to be able to register a library that is loaded automatically
399+ when accessing a particular top-level constant for the first
397400time. This can be used like this:
398401
399402<pre class="emlist">
@@ -407,7 +410,9 @@ an efficient approach, when a library is too big and a lot of time is spent on l
407410
408411This autoload is processed by `rb_autoload_xxxx()`. We won't discuss
409412autoload further in this chapter because there will probably be a big
410- change in how it works soon (The way autoload works _did_ change in
413+ change in how it works soon.
414+
415+ (translator's note: The way autoload works _did_ change in
4114161.8: autoloaded constants do not need to be defined at top-level
412417anymore).
413418
@@ -436,7 +441,8 @@ there's no need to do any check. Therefore implementation is very
436441simple.
437442
438443But there is still quite a lot of code. The reason for this is that global
439- variables are quite different from normal variables. Functions like
444+ variables of Ruby are equipped with some gimmicks which make it hard to regard
445+ them as mere variables. Functions like
440446the following are only available for global variables:
441447
442448* you can "hook" access of global variables
@@ -468,12 +474,14 @@ h4. Hooks
468474
469475You can "hook" read and write of global variables.
470476
471- Hooks can be also be set at the Ruby level, but I was thinking: why not
472- instead look at C level special variables for system use like
473- `$KCODE`? `$KCODE` is the variable containing the encoding the
474- interpreter currently uses to handle strings. It can only be set to
475- special values like `"EUC"` or `"UTF8"`. But this is too bothersome so
476- it can also be set it to `"e"` or `"u"`.
477+ Although hooks can be also be set at the Ruby level,
478+ I think the purpose of it seems rather to prepare the special variables
479+ for system use like `$KCODE` at C level.
480+ `$KCODE` is the variable containing the encoding the
481+ interpreter currently uses to handle strings.
482+ Essentially only special strings like `"EUC"` or `"UTF8"` can be assigned to
483+ it, but this is too bothersome so it is designed so that `"e"` or `"u"` can
484+ also be used.
477485
478486<pre class="emlist">
479487p($KCODE) # "NONE" (default)
@@ -490,14 +498,14 @@ from "kanji" (the name of Chinese characters in Japanese).
490498You might say that even with `alias` or hooks,
491499global variables just aren't used much, so it's functionality that doesn't
492500really mater. It's adequate not to talk much about unused
493- functions, and I need some pages for the analysis of the parser and
494- evaluator. That's why I'll proceed with the explanation below throwing
495- away what's not really important .
501+ functions, and I'd like to use more pages for the analysis of the parser and
502+ evaluator. That's why I'll proceed with the explanation below
503+ whose degree of half-hearted is 85% .
496504
497505h3. Data structure
498506
499- When we were looking at how variables work, I said that the way they
500- are stored is important. That's why I'd like you to firmly grasp the
507+ I said that the point when looking at how variables work is the way they
508+ are stored. First, I'd like you to firmly grasp the
501509structure used by global variables.
502510
503511▼ Data structure for global variables
@@ -622,16 +630,15 @@ created, `rb_global_entry()` will never return NULL.
622630
623631This was mainly done for speed. When the parser finds a global
624632variable, it gets the corresponding `struct global_entry`. When
625- reading the value of the variable, the parser just has to get the
626- value from the entry (using `rb_gv_get()`), and has no need to do any
627- check.
633+ reading the value of the variable, the value is just obtained from the entry
634+ (using `rb_gv_get()`).
628635
629636Let's now continue a little with the code that follows. `var->getter`
630- and others are set to `undef_xxxx`. `undef` means that the global
631- `setter/getter/marker` for the variable are currently undefined.
637+ and others are set to `undef_xxxx`. `undef` probably means that they are
638+ the `setter/getter/marker` for a global variable whose state is undefined.
632639
633640`undef_getter()` just shows a warning and returns `nil`, as even
634- undefined global variables can be read. `undef_setter()` is quite
641+ undefined global variables can be read. `undef_setter()` is a little bit
635642interesting so let's look at it.
636643
637644▼ `undef_setter()`
0 commit comments