@@ -10,25 +10,27 @@ h2. Structure of Ruby objects
1010
1111h3. Guideline
1212
13- In this chapter we will begin exploring the `ruby` source code, starting by
14- studying the declaration of object structures.
13+ From this chapter, we will begin actually exploring the `ruby` source code.
14+ First, as declared at the beginning of this book,
15+ we'll start with the object structure.
1516
16- What do objects need to exist? There are many answers to this question, but for
17- our purposes an object only needs three things:
17+ What are the necessary conditions for objects to be objects?
18+ There could be many ways to explain about object itself,
19+ but there are only three conditions that are truly indispensable.
1820
1921# The ability to differentiate itself from other objects (an identity)
2022# The ability to respond to messages (methods)
2123# The ability to store internal state (instance variables)
2224
2325In this chapter, we are going to confirm these three features one by one.
2426
25- The most interesting file in this quest will be `ruby.h`, but we will also
27+ The target file is mainly `ruby.h`, but we will also
2628briefly look at other files such as `object.c`, `class.c` or `variable.c`.
2729
28- h3. Structure of `VALUE` and objects
30+ h3. `VALUE` and object struct
2931
30- In `ruby`, the contents of an object are expressed by a `C` structure, always
31- handled via a pointer. A different kind of structure is used for each class, but
32+ In `ruby`, the body of an object is expressed by a struct and always
33+ handled via a pointer. A different struct type is used for each class, but
3234the pointer type will always be `VALUE` (figure 1).
3335
3436!images/ch_object_value.png(`VALUE` and structure)!
@@ -43,13 +45,15 @@ Here is the definition of `VALUE`:
4345(ruby.h)
4446</pre>
4547
46- In practice, a `VALUE` must be cast to different types of structure pointer .
48+ In practice, when using a `VALUE`, we cast it to the pointer to each object struct .
4749Therefore if an `unsigned long` and a pointer have a different size, `ruby`
48- will not work well. Strictly speaking, it will not work for pointer types
49- bigger than `sizeof(unsigned long)`. Fortunately, no recent machine feature
50- this capability, even if some time ago there were quite a few of them.
50+ will not work well. Strictly speaking, it will not work if there's a pointer
51+ type that is bigger than `sizeof(unsigned long)`. Fortunately, systems which
52+ could not meet this requirement is unlikely recently,
53+ but some time ago it seems there were quite a few of them.
5154
52- Several structures are available according to object classes:
55+ The structs, on the other hand, have several variations,
56+ a different struct is used based on the class of the object.
5357
5458| `struct RObject` | all things for which none of the following
5559 applies |
@@ -110,7 +114,8 @@ Before looking at every one of them in detail, let's begin with something more
110114general.
111115
112116First, as `VALUE` is defined as `unsigned long`, it must be cast before
113- being used. That's why `Rxxxx()` macros have been made for each object
117+ being used when it is used as a pointer.
118+ That's why `Rxxxx()` macros have been made for each object
114119structure. For example, for `struct RString` there is `RSTRING()`, for
115120`struct RArray` there is `RARRAY()`, etc... These macros are used like this:
116121
@@ -129,8 +134,9 @@ of the type of structure pointed to by `VALUE`.
129134
130135!images/ch_object_rbasic.png(`struct RBasic`)!
131136
132- You probably guessed that `struct RBasic` has been designed to contain some
133- important information shared by all object structures. Here is the definition
137+ Because it is purposefully designed this way,
138+ `struct RBasic` must contain very important information for Ruby objects.
139+ Here is the definition
134140for `struct RBasic`:
135141
136142▼ `struct RBasic`
@@ -154,8 +160,9 @@ str = rb_str_new(); /* creates a Ruby string (its structure is RString) */
154160TYPE(str); /* the return value is T_STRING */
155161</pre>
156162
157- The names of these `T_xxxx` flags are directly linked to the corresponding type
158- name, like `T_STRING` for `struct RString` and `T_ARRAY` for `struct RArray`.
163+ The all flags are named as `T_xxxx`,
164+ like `T_STRING` for `struct RString` and `T_ARRAY` for `struct RArray`.
165+ They are very straightforwardly corresponded to the type names.
159166
160167The other member of `struct RBasic`, `klass`, contains the class this object
161168belongs to. As the `klass` member is of type `VALUE`, what is stored is (a
@@ -199,10 +206,11 @@ easier and faster to put the information about the type in the structure.
199206
200207h4. The use of `basic.flags`
201208
202- As limiting myself to saying that `basic.flags` is used for different things
203- including the type of structure makes me feel bad, here's a general
204- illustration for it (figure 5). There is no need to understand everything
205- right away, I just wanted to show its uses while it was bothering me.
209+ Regarding the use of `basic.flags`,
210+ because I feel bad to say it is the structure type "and such",
211+ I'll illustrate it entirely here. (Figure 5)
212+ There is no need to understand everything right away,
213+ because this is prepared for the time when you will be wondering about it later.
206214
207215!images/ch_object_flags.png(Use of `flags`)!
208216
@@ -229,13 +237,14 @@ I'll explain them one by one.
229237
230238h4. Small integers
231239
232- Just like in Ruby itself, all data are objects. Likewise, integers are objects.
233- However during normal program execution, lots of instances of integers are
234- created. Using structures to express them would risk slowing down execution. For
235- example, if we created 50000 objects when incrementing from 0 to 50000 we'd
236- definitely have to consider the performance issues.
240+ All data are objects in Ruby, thus integers are also objects.
241+ But since there are so many kind of integer objects,
242+ if each of them is expressed as a structure,
243+ it would risk slowing down execution significantly.
244+ For example, when incrementing from 0 to 50000,
245+ we would hesitate to create 50000 objects for only that purpose.
237246
238- That's why in `ruby`, to some extent, small integers are treated specially and
247+ That's why in `ruby`, integers that are small to some extent are treated specially and
239248embedded directly into `VALUE`. "Small" means signed integers that can be held
240249in `sizeof(VALUE)*8-1` bits. In other words, on 32 bits machines, the integers
241250have 1 bit for the sign, and 30 bits for the integer part. Integers in this
@@ -277,8 +286,8 @@ h4. Symbols
277286What are symbols?
278287
279288As this question is quite troublesome to answer, let's start with the reasons
280- why symbols were necessary. First, let's start with the `ID` type used inside
281- ` ruby`. It's like this:
289+ why symbols were necessary.
290+ In the first place, there's a type named `ID` used inside ` ruby`. Here it is.
282291
283292▼ `ID`
284293
@@ -289,13 +298,13 @@ why symbols were necessary. First, let's start with the `ID` type used inside
289298</pre>
290299
291300This `ID` is a number having a one-to-one association with a string. However,
292- in this world it's not possible to have an association between all strings and
293- a numerical value. That's why they are limited to the one to one relationships
301+ it's not possible to have an association between all strings in this world and
302+ numerical values. It is limited to the one to one relationships
294303inside one `ruby` process. I'll speak of the method to find an `ID` in the
295304next chapter "Names and name tables".
296305
297- In language implementations , there are a lot of names to handle. Method names
298- or variable names, constant names, file names in class names... It's
306+ In language processor , there are a lot of names to handle. Method names
307+ or variable names, constant names, file names, class names... It's
299308troublesome to handle all of them as strings (`char*`), because of memory
300309management and memory management and memory management... Also, lots of
301310comparisons would certainly be necessary, but comparing strings character by
@@ -304,14 +313,14 @@ directly, something will be associated and used instead. And generally that
304313"something" will be integers, as they are the simplest to handle.
305314
306315These `ID` are found as symbols in the Ruby world. Up to `ruby 1.4`, the
307- values of `ID` were converted to `Fixnum`, but used as symbols. Even today
316+ values of `ID` converted to `Fixnum` were used as symbols. Even today
308317these values can be obtained using `Symbol#to_i`. However, as real use results
309318came piling up, it was understood that making `Fixnum` and `Symbol` the same
310319was not a good idea, so since 1.6 an independent class `Symbol` has been
311320created.
312321
313322`Symbol` objects are used a lot, especially as keys for hash tables. That's
314- why `Symbol`, like `Fixnum`, was made stored in `VALUE`. Let's look at the
323+ why `Symbol`, like `Fixnum`, was made embedded in `VALUE`. Let's look at the
315324`ID2SYM()` macro converting `ID` to `Symbol` object.
316325
317326▼ `ID2SYM`
@@ -358,7 +367,7 @@ values at the C level are defined like this:
358367(ruby.h)
359368</pre>
360369This time it's even numbers, but as 0 or 2 can't be used by pointers, they
361- can't overlap with other `VALUE`. It's because usually the first bloc of
370+ can't overlap with other `VALUE`. It's because usually the first block of
362371virtual memory is not allocated, to make the programs dereferencing a `NULL`
363372pointer crash.
364373
@@ -382,10 +391,10 @@ function returning a boolean value. In other words, `NIL_P` means "is the
382391argument `nil`?". It seems the "`p`" character comes from "predicate." This
383392naming rule is used at many different places in `ruby`.
384393
385- Also, in Ruby, `false` and `nil` are falsy (that is, they count as false in
386- conditional statements) and all the other objects are truthy .
387- However, in C, `nil` (`Qnil`) is true. That's why in C a Ruby-style macro,
388- `RTEST()`, has been created .
394+ Also, in Ruby, `false` and `nil` are false (in conditional statements)
395+ and all the other objects are true .
396+ However, in C, `nil` (`Qnil`) is true.
397+ That's why there's the `RTEST()` macro to do Ruby-style test in C .
389398
390399▼ `RTEST()`
391400
@@ -417,7 +426,7 @@ h4. `Qundef`
417426</pre>
418427
419428This value is used to express an undefined value in the interpreter. It can't
420- be found at all at the Ruby level.
429+ (must not) be found at all at the Ruby level.
421430
422431h2. Methods
423432
@@ -432,7 +441,7 @@ In Ruby, classes exist as objects during the execution. Of course. So there
432441must be a structure for class objects. That structure is `struct RClass`. Its
433442structure type flag is `T_CLASS`.
434443
435- As class and modules are very similar, there is no need to differentiate their
444+ As classes and modules are very similar, there is no need to differentiate their
436445content. That's why modules also use the `struct RClass` structure, and are
437446differentiated by the `T_MODULE` structure flag.
438447
@@ -455,6 +464,8 @@ next chapter "Names and name tables", but basically, it is a table mapping
455464names to objects. In the case of `m_tbl`, it keeps the
456465correspondence between the name (`ID`) of the methods possessed by this class
457466and the methods entity itself.
467+ As for the structure of the method entity,
468+ it will be explained in Part 2 and Part 3.
458469
459470The fourth member `super` keeps, like its name suggests, the superclass. As
460471it's a `VALUE`, it's (a pointer to) the class object of the superclass. In Ruby
@@ -463,14 +474,14 @@ there is only one class that has no superclass (the root class): `Object`.
463474However I already said that all `Object` methods are defined in the `Kernel`
464475module, `Object` just includes it. As modules are functionally similar to
465476multiple inheritance, it may seem having just `super` is problematic, but
466- in `ruby` some clever changes are made to make it look like single
477+ in `ruby` some clever conversions are made to make it look like single
467478inheritance. The details of this process will be explained in the fourth
468479chapter "Classes and modules".
469480
470- Because of this, `super` of the structure of `Object` points to `struct RClass`
471- of the `Kernel` object. Only the `super` of Kernel is NULL. So contrary to
472- what I said , if `super` is NULL, this `RClass` is the `Kernel` object (figure
473- 6).
481+ Because of this conversion , `super` of the structure of `Object` points to `struct RClass`
482+ which is the entity of `Kernel` object and the `super` of Kernel is ` NULL`.
483+ So to put it conversely , if `super` is ` NULL`,
484+ its `RClass` is the entity of `Kernel` (figure 6).
474485
475486!images/ch_object_classtree.png(Class tree at the C level)!
476487
0 commit comments