Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Ruby Programming
Previous Page Home Next Page

VALUE as a Pointer

When VALUE is a pointer, it is a pointer to one of the defined Ruby object structures---you can't have a VALUE that points to an arbitrary structure. The structures for each built-in class are defined in ``ruby.h'' and are named R Classname, as in RString and RArray.

You can check to see what type of structure is used for a particular VALUE in a number of ways. The macro TYPE( obj ) will return a constant representing the C type of the given object: T_OBJECT, T_STRING, and so on. Constants for the built-in classes are defined in ``ruby.h''. Note that the type we are referring to here is an implementation detail---it is not the same as the class of an object.

If you want to ensure that a value pointer points to a particular structure, you can use the macro Check_Type, which will raise a TypeError exception if value is not of the expected type (which is one of the constants T_STRING, T_FLOAT, and so on):

Check_Type(VALUE value, int type)

If speed is an issue, there are faster macros that check specifically for the immediate values Fixnum and nil.

FIXNUM_P(value) -> non-zero if value is a Fixnum
NIL_P(value)    -> non-zero if value is nil
RTEST(value)    -> non-zero if value is neither nil nor false

Again, note that we are talking about ``type'' as the C structure that represents a particular built-in type. The class of an object is a different beast entirely. The class objects for the built-in classes are stored in C global variables named rb_c Classname (for instance, rb_cObject); modules are named rb_m Modulename.

It wouldn't be advisable to mess with the data in these structures directly, however---you may look, but don't touch unless you are fond of debuggers. You should normally use only the supplied C functions to manipulate Ruby data (we'll talk more about this in just a moment).

However, in the interests of efficiency you may need to dig into these structures to obtain data. In order to dereference members of these C structures, you have to cast the generic VALUE to the proper structure type. ruby.h contains a number of macros that perform the proper casting for you, allowing you to dereference structure members easily. These macros are named RCLASSNAME , as in RSTRING or RARRAY. For example:

VALUE str, arr;
RSTRING(str)->len -> length of the Ruby string
RSTRING(str)->ptr -> pointer to string storage
RARRAY(arr)->len  -> length of the Ruby array
RARRAY(arr)->capa -> capacity of the Ruby array
RARRAY(arr)->ptr  -> pointer to array storage
Ruby Programming
Previous Page Home Next Page

 
 
  Published under the terms of the Open Publication License Design by Interspire