#define R_CAST(st) (struct st*)
#define RBASIC(obj) (R_CAST(RBasic)(obj))
#define ROBJECT(obj) (R_CAST(RObject)(obj))
#define RCLASS(obj) (R_CAST(RClass)(obj))
#define RMODULE(obj) RCLASS(obj)
#define RFLOAT(obj) (R_CAST(RFloat)(obj))
#define RSTRING(obj) (R_CAST(RString)(obj))
#define RREGEXP(obj) (R_CAST(RRegexp)(obj))
#define RARRAY(obj) (R_CAST(RArray)(obj))
#define RHASH(obj) (R_CAST(RHash)(obj))
#define RDATA(obj) (R_CAST(RData)(obj))
#define RSTRUCT(obj) (R_CAST(RStruct)(obj))
#define RBIGNUM(obj) (R_CAST(RBignum)(obj))
#define RFILE(obj) (R_CAST(RFile)(obj))
In ruby, the contents of an object is expressed by a C structure, always handled via a pointer. A different kind of structure is used for each class, but the pointer type will always be VALUE (figure 1).
You guessed that struct RBasic has been designed to contain some important information shared by all object structures. The definition of struct RBasic is the following:
▼ struct RBasic
290 struct RBasic {
291 unsigned long flags;
292 VALUE klass;
293 };
(ruby.h)
flags are multipurpose flags, mostly used to register the structure type (for instance struct RObject). The type flags are named T_xxxx, and can be obtained from a VALUE using the macro TYPE(). Here is an example:
static inline int
#if defined(HAVE_PROTOTYPES)
rb_type(VALUE obj)
#else
rb_type(obj)
VALUE obj;
#endif
{
if (FIXNUM_P(obj)) return T_FIXNUM;
if (obj == Qnil) return T_NIL;
if (obj == Qfalse) return T_FALSE;
if (obj == Qtrue) return T_TRUE;
if (obj == Qundef) return T_UNDEF;
if (SYMBOL_P(obj)) return T_SYMBOL;
return BUILTIN_TYPE(obj);
}
#define TYPE(x) rb_type((VALUE)(x))
#define T_MASK 0x3f
#define BUILTIN_TYPE(x) (((struct RBasic*)(x))->flags & T_MASK)
VALUE str;
str = rb_str_new(); /* creates a Ruby string (its structure is RString) */
TYPE(str); /* the return value is T_STRING */
The names of these T_xxxx flags are directly linked to the corresponding type name, like T_STRING for struct RString and T_ARRAY for struct RArray.
The other member of struct RBasic, klass, contains the class this object belongs to. As the klass member is of type VALUE, what is stored is (a pointer to) a Ruby object. In short, it is a class object.
分享到:
相关推荐
Objects and Lvalues Conversions Expressions Declarations Statements External Declarations Scope and Linkage Preprocessor Grammar Appendix B: Standard Library Input and Output: <stdio.h> ...
- **Classes and Objects**: Explanation of classes and objects, including encapsulation, inheritance, and polymorphism. - **Public and Private Members**: Discussion of access specifiers, particularly ...
Explore Swift's features including its static type system, value objects, and functional programming Design reusable code for high performance in Swift Use to Xcode LLBD and REPL to debug commands ...
Value arrays - A container structure to maintain an array of generic values III. Tools Reference glib-mkenums - C language enum description generation utility glib-genmarshal - C code marshaller ...
The availability of drivers that support the features of the specification will simplify the process of developing Windows ...and enhance the acceptance of ad¬vanced pointing de¬vices among users....
Auto-select of optimal algorithm to transfer data reliably based on table structure, database engine and database version Speeds of over 30 thousand records per second reached in favorable ...
It’s also useful in generating value-lists, in dynamic pivoting of data for business intelligence reporting, and for customizing database objects and querying their structure. Executing dynamic SQL...
C# is an object-oriented programming (OOP) language, which means that it structures code around objects rather than actions and data instead of logic. Object orientation provides several key benefits,...
- This involves solving boundary value problems (interpolation) and initial value problems (extrapolation) to achieve natural extensions of shape sequences without requiring semantic information, ...
Deprecated functions and types of the C API Deprecated Build Options Removed API and Feature Removals Porting to Python 3.6 Changes in ‘python’ Command Behavior Changes in the Python API ...
ChaptEr 15 Classes and Objects 551 appEndix a C++ keywords 593 appEndix B aSCii Codes 595 appEndix C Common Syntax errors 597 appEndix d How To Boxes 599 index 603 The answers pdf and data files can ...
- FIX: In "Windows ClearType" font rendering mode (OS Windows mode) the "garbage" pixels can appear from the right and from the bottom sides of the painted rectangle of the TFlexText object....
- **Morphological Transforms:** Operations like dilation and erosion that manipulate the shape and size of objects in an image. #### 4. Cameras, Stereopsis, and Reconstruction ##### 4.1 A Single ...
the difficulties which bring by the huge dimensions and sparsity of text objects, but also retains the high performance of k-Means. At the same time, the gradual approach strategy also solves the ...
Principles and Structure of Programs #### A. Objects ##### 1. In Everyday Life Objects are real-world entities that can be represented in programming. For example, a car can be considered an object ...