@@ -10,16 +10,15 @@ h2. Structure of Ruby objects
1010
1111h3. Guideline
1212
13- Starting from this chapter we will explore the `ruby` source code, starting by
14- studying the declaration of objects structures.
13+ In this chapter we will begin exploring the `ruby` source code, starting by
14+ studying the declaration of object structures.
1515
16- What are the required conditions to make sure objects can exist? Many
17- explanations can be given but in reality there are three conditions that must
18- be obeyed:
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:
1918
20- # Being able to differentiate itself from the rest (having an identity)
21- # Being able to reply to requests (methods)
22- # Keeping an internal state (instance variables)
19+ # The ability to differentiate itself from other objects ( an identity)
20+ # The ability to respond to messages (methods)
21+ # The ability to store internal state (instance variables)
2322
2423In this chapter, we are going to confirm these three features one by one.
2524
@@ -28,9 +27,9 @@ briefly look at other files such as `object.c`, `class.c` or `variable.c`.
2827
2928h3. Structure of `VALUE` and objects
3029
31- In `ruby`, the contents of an object is expressed by a `C` structure, always
32- handled via a pointer. A different kind of structure is used for each class,
33- but the pointer type will always be `VALUE` (figure 1).
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+ the pointer type will always be `VALUE` (figure 1).
3433
3534!images/ch_object_value.png(`VALUE` and structure)!
3635
@@ -124,15 +123,15 @@ RARRAY(arr)->len; /* ((struct RArray*)arr)->len */
124123</pre>
125124
126125Another important point to mention is that all object structures start with a
127- member `basic` of type `struct RBasic`. As a result, whatever the type of
128- structure pointed by `VALUE`, if you cast this `VALUE` to `struct RBasic*`,
129- you will be able to access the content of `basic `.
126+ member `basic` of type `struct RBasic`. As a result, if you cast this `VALUE` to
127+ `struct RBasic*`, you will be able to access the content of `basic`, regardless
128+ of the type of structure pointed to by `VALUE `.
130129
131130!images/ch_object_rbasic.png(`struct RBasic`)!
132131
133- You guessed that `struct RBasic` has been designed to contain some important
134- information shared by all object structures. The definition of `struct RBasic`
135- is the following :
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
134+ for `struct RBasic` :
136135
137136▼ `struct RBasic`
138137
@@ -167,9 +166,8 @@ pointer to) a Ruby object. In short, it is a class object.
167166The relation between an object and its class will be detailed in the "Methods"
168167section of this chapter.
169168
170- By the way, the name of this member is not `class` to make sure it does not
171- raise any conflict when the file is processed by a C++ compiler, as it is a
172- reserved word.
169+ By the way, this member is named `klass` so as not to conflict with the reserved
170+ word `class` when the file is processed by a C++ compiler.
173171
174172h4. About structure types
175173
@@ -231,19 +229,20 @@ I'll explain them one by one.
231229
232230h4. Small integers
233231
234- As in Ruby all data are objects, integers are also objects. However, as there
235- are lots of different instances of integers, expressing them as structures
236- would risk slowing down execution. For example, when incrementing from 0
237- to 50000, just for this creating 50000 objects would make us hesitate.
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.
238237
239238That's why in `ruby`, to some extent, small integers are treated specially and
240- embedded directly into `VALUE`. "small " means signed integers that can be held
239+ embedded directly into `VALUE`. "Small " means signed integers that can be held
241240in `sizeof(VALUE)*8-1` bits. In other words, on 32 bits machines, the integers
242241have 1 bit for the sign, and 30 bits for the integer part. Integers in this
243242range will belong to the `Fixnum` class and the other integers will belong to
244243the `Bignum` class.
245244
246- Then, let 's see in practice the `INT2FIX()` macro that converts from a C `int`
245+ Let 's see in practice the `INT2FIX()` macro that converts from a C `int`
247246to a `Fixnum`, and confirm that `Fixnum` are directly embedded in `VALUE`.
248247
249248▼ `INT2FIX`
@@ -271,7 +270,7 @@ containing `NUM` can manage both `Fixnum` and `Bignum`. For example if
271270`INT2NUM()` can't convert an integer into a `Fixnum`, it will automatically
272271convert it to `Bignum`. `NUM2INT()` will convert both `Fixnum` and `Bignum` to
273272`int`. If the number can't fit in an `int`, an exception will be raised, so
274- there is not need to check the value range.
273+ there is no need to check the value range.
275274
276275h4. Symbols
277276
@@ -301,11 +300,11 @@ troublesome to handle all of them as strings (`char*`), because of memory
301300management and memory management and memory management... Also, lots of
302301comparisons would certainly be necessary, but comparing strings character by
303302character will slow down the execution. That's why strings are not handled
304- directly, something will be associated and used instead. And generally
303+ directly, something will be associated and used instead. And generally that
305304"something" will be integers, as they are the simplest to handle.
306305
307306These `ID` are found as symbols in the Ruby world. Up to `ruby 1.4`, the
308- values of `ID` where converted to `Fixnum`, but used as symbols. Even today
307+ values of `ID` were converted to `Fixnum`, but used as symbols. Even today
309308these values can be obtained using `Symbol#to_i`. However, as real use results
310309came piling up, it was understood that making `Fixnum` and `Symbol` the same
311310was not a good idea, so since 1.6 an independent class `Symbol` has been
@@ -380,10 +379,11 @@ For `Qnil`, there is a macro dedicated to check if a `VALUE` is `Qnil` or not,
380379
381380The name ending with `p` is a notation coming from Lisp denoting that it is a
382381function returning a boolean value. In other words, `NIL_P` means "is the
383- argument `nil`?". It seems the "`p`" character comes from "predicate". This
382+ argument `nil`?". It seems the "`p`" character comes from "predicate." This
384383naming rule is used at many different places in `ruby`.
385384
386- Also, in Ruby, `false` and `nil` are false and all the other objects are true.
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.
387387However, in C, `nil` (`Qnil`) is true. That's why in C a Ruby-style macro,
388388`RTEST()`, has been created.
389389
@@ -403,7 +403,7 @@ requirements of the glib library that only wants 0 or 1
403403("[ruby-dev:11049]":http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-dev/11049).
404404
405405By the way, what is the '`Q`' of `Qnil`? 'R' I would have understood but why
406- '`Q`'? When I asked, the answer was "Because it's like that in Emacs". I did
406+ '`Q`'? When I asked, the answer was "Because it's like that in Emacs." I did
407407not have the fun answer I was expecting...
408408
409409h4. `Qundef`
@@ -421,10 +421,10 @@ be found at all at the Ruby level.
421421
422422h2. Methods
423423
424- I already brought up the three important points of a Ruby object, that is
425- having an identity, being able to call a method, and keeping data for each
426- instance. In this section, I'll explain in a simple way the structure linking
427- objects and methods.
424+ I already brought up the three important points of a Ruby object: having an
425+ identity, being able to call a method, and keeping data for each instance. In
426+ this section, I'll explain in a simple way the structure linking objects and
427+ methods.
428428
429429h3. `struct RClass`
430430
0 commit comments