1 .\" README.EXT - -*- Text -*- created at: Mon Aug 7 16:45:54 JST 1995
3 This document explains how to make extension libraries for Ruby.
7 In C, variables have types and data do not have types. In contrast,
8 Ruby variables do not have a static type, and data themselves have
9 types, so data will need to be converted between the languages.
11 Data in Ruby are represented by the C type `VALUE'. Each VALUE data
14 To retrieve C data from a VALUE, you need to:
16 (1) Identify the VALUE's data type
17 (2) Convert the VALUE into C data
19 Converting to the wrong data type may cause serious problems.
24 The Ruby interpreter has the following data types:
27 T_OBJECT ordinary object
30 T_FLOAT floating point number
32 T_REGEXP regular expression
34 T_FIXNUM Fixnum(31bit or 63bit integer)
35 T_HASH associative array
36 T_STRUCT (Ruby) structure
37 T_BIGNUM multi precision integer
44 In addition, there are several other types used internally:
53 Most of the types are represented by C structures.
55 1.2 Check Data Type of the VALUE
57 The macro TYPE() defined in ruby.h shows the data type of the VALUE.
58 TYPE() returns the constant number T_XXXX described above. To handle
59 data types, your code will look something like this:
73 rb_raise(rb_eTypeError, "not valid value");
77 There is the data-type check function
79 void Check_Type(VALUE value, int type)
81 which raises an exception if the VALUE does not have the type
84 There are also faster check macros for fixnums and nil.
89 1.3 Convert VALUE into C data
91 The data for type T_NIL, T_FALSE, T_TRUE are nil, true, false
92 respectively. They are singletons for the data type.
94 The T_FIXNUM data is a 31bit length fixed integer (63bit length on
95 some machines), which can be converted to a C integer by using the
96 FIX2INT() macro or FIX2LONG(). Though you have to check that the
97 data is really FIXNUM before using them, they are faster. FIX2LONG()
98 never raises exceptions, but FIX2INT() raises RangeError if the
99 result is bigger or smaller than the size of int.
100 There are also NUM2INT() and NUM2LONG() which converts any Ruby
101 numbers into C integers. These macros includes a type check,
102 so an exception will be raised if the conversion failed. NUM2DBL()
103 can be used to retrieve the double float value in the same way.
105 In version 1.7 or later it is recommended that you use the new macros
106 StringValue() and StringValuePtr() to get a char* from a VALUE.
107 StringValue(var) replaces var's value with the result of "var.to_str()".
108 StringValuePtr(var) does same replacement and returns char*
109 representation of var. These macros will skip the replacement if var
110 is a String. Notice that the macros take only the lvalue as their
111 argument, to change the value of var in place.
113 You can also use the macro named StringValueCStr(). This is just
114 like StringValuePtr(), but always add nul character at the end of
115 the result. If the result contains nul character, this macro causes
116 the ArgumentError exception.
117 StringValuePtr() doesn't gurantee to exist nul at the end of the
118 result, and the result may contain nul.
120 In version 1.6 or earlier, STR2CSTR() was used to do the same thing
121 but now it is deprecated in version 1.7, because STR2CSTR() has a risk
122 of a dangling pointer problem in the to_str() implicit conversion.
124 Other data types have corresponding C structures, e.g. struct RArray
125 for T_ARRAY etc. The VALUE of the type which has the corresponding
126 structure can be cast to retrieve the pointer to the struct. The
127 casting macro will be of the form RXXXX for each data type; for
128 instance, RARRAY(obj). See "ruby.h".
130 There are some accessing macros for structure members, for example
131 `RSTRING_LEN(s)' to to get the size of the Ruby String object. The
132 allocated region can be accessed by `RSTRING_PTR(str). For arrays, use
133 `RARRAY_LEN(ary) and `RARRAY_PTR(ary) respectively.
135 Notice: Do not change the value of the structure directly, unless you
136 are responsible for the result. This ends up being the cause of
139 1.4 Convert C data into VALUE
141 To convert C data to Ruby values:
145 left shift 1 bit, and turn on LSB.
147 * Other pointer values
151 You can determine whether a VALUE is pointer or not by checking its LSB.
153 Notice Ruby does not allow arbitrary pointer values to be a VALUE. They
154 should be pointers to the structures which Ruby knows about. The known
155 structures are defined in <ruby.h>.
157 To convert C numbers to Ruby values, use these macros.
159 INT2FIX() for integers within 31bits.
160 INT2NUM() for arbitrary sized integer.
162 INT2NUM() converts an integer into a Bignum if it is out of the FIXNUM
163 range, but is a bit slower.
165 1.5 Manipulating Ruby data
167 As I already mentioned, it is not recommended to modify an object's
168 internal structure. To manipulate objects, use the functions supplied
169 by the Ruby interpreter. Some (not all) of the useful functions are
174 rb_str_new(const char *ptr, long len)
176 Creates a new Ruby string.
178 rb_str_new2(const char *ptr)
180 Creates a new Ruby string from a C string. This is equivalent to
181 rb_str_new(ptr, strlen(ptr)).
183 rb_tainted_str_new(const char *ptr, long len)
185 Creates a new tainted Ruby string. Strings from external data
186 sources should be tainted.
188 rb_tainted_str_new2(const char *ptr)
190 Creates a new tainted Ruby string from a C string.
192 rb_sprintf(const char *format, ...)
193 rb_vsprintf(const char *format, va_list ap)
195 Creates a new Ruby string with printf(3) format.
197 rb_str_cat(VALUE str, const char *ptr, long len)
199 Appends len bytes of data from ptr to the Ruby string.
201 rb_str_cat2(VALUE str, const char* ptr)
203 Appends C string ptr to Ruby string str. This function is
204 equivalent to rb_str_cat(str, ptr, strlen(ptr)).
206 rb_str_catf(VALUE str, const char* format, ...)
207 rb_str_vcatf(VALUE str, const char* format, va_list ap)
209 Appends C string format and successive arguments to Ruby string
210 str according to a printf-like format. These functions are
211 equivalent to rb_str_cat2(str, rb_sprintf(format, ...)) and
212 rb_str_cat2(str, rb_vsprintf(format, ap)), respectively.
218 Creates an array with no elements.
220 rb_ary_new2(long len)
222 Creates an array with no elements, allocating internal buffer
225 rb_ary_new3(long n, ...)
227 Creates an n-element array from the arguments.
229 rb_ary_new4(long n, VALUE *elts)
231 Creates an n-element array from a C array.
233 rb_ary_push(VALUE ary, VALUE val)
234 rb_ary_pop(VALUE ary)
235 rb_ary_shift(VALUE ary)
236 rb_ary_unshift(VALUE ary, VALUE val)
238 Array operations. The first argument to each functions must be an
239 array. They may dump core if other types are given.
241 2. Extending Ruby with C
243 2.1 Adding new features to Ruby
245 You can add new features (classes, methods, etc.) to the Ruby
246 interpreter. Ruby provides APIs for defining the following things:
249 * Methods, Singleton Methods
252 2.1.1 Class/module definition
254 To define a class or module, use the functions below:
256 VALUE rb_define_class(const char *name, VALUE super)
257 VALUE rb_define_module(const char *name)
259 These functions return the newly created class or module. You may
260 want to save this reference into a variable to use later.
262 To define nested classes or modules, use the functions below:
264 VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
265 VALUE rb_define_module_under(VALUE outer, const char *name)
267 2.1.2 Method/singleton method definition
269 To define methods or singleton methods, use these functions:
271 void rb_define_method(VALUE klass, const char *name,
272 VALUE (*func)(), int argc)
274 void rb_define_singleton_method(VALUE object, const char *name,
275 VALUE (*func)(), int argc)
277 The `argc' represents the number of the arguments to the C function,
278 which must be less than 17. But I doubt you'll need that many.
280 If `argc' is negative, it specifies the calling sequence, not number of
283 If argc is -1, the function will be called as:
285 VALUE func(int argc, VALUE *argv, VALUE obj)
287 where argc is the actual number of arguments, argv is the C array of
288 the arguments, and obj is the receiver.
290 If argc is -2, the arguments are passed in a Ruby array. The function
293 VALUE func(VALUE obj, VALUE args)
295 where obj is the receiver, and args is the Ruby array containing
298 There are two more functions to define methods. One is to define
301 void rb_define_private_method(VALUE klass, const char *name,
302 VALUE (*func)(), int argc)
304 The other is to define module functions, which are private AND singleton
305 methods of the module. For example, sqrt is the module function
306 defined in Math module. It can be called in the following way:
315 To define module functions, use:
317 void rb_define_module_function(VALUE module, const char *name,
318 VALUE (*func)(), int argc)
320 Oh, in addition, function-like methods, which are private methods defined
321 in the Kernel module, can be defined using:
323 void rb_define_global_function(const char *name, VALUE (*func)(), int argc)
325 To define an alias for the method,
327 void rb_define_alias(VALUE module, const char* new, const char* old);
329 To define and undefine the `allocate' class method,
331 void rb_define_alloc_func(VALUE klass, VALUE (*func)(VALUE klass));
332 void rb_undef_alloc_func(VALUE klass);
334 func have to take the klass as the argument and return a newly
335 allocated instance. This instance should be empty as possible,
336 without any expensive (including external) resources.
338 2.1.3 Constant definition
340 We have 2 functions to define constants:
342 void rb_define_const(VALUE klass, const char *name, VALUE val)
343 void rb_define_global_const(const char *name, VALUE val)
345 The former is to define a constant under specified class/module. The
346 latter is to define a global constant.
348 2.2 Use Ruby features from C
350 There are several ways to invoke Ruby's features from C code.
352 2.2.1 Evaluate Ruby Programs in a String
354 The easiest way to use Ruby's functionality from a C program is to
355 evaluate the string as Ruby program. This function will do the job:
357 VALUE rb_eval_string(const char *str)
359 Evaluation is done under the current context, thus current local variables
360 of the innermost method (which is defined by Ruby) can be accessed.
364 You can invoke methods directly, without parsing the string. First I
365 need to explain about ID. ID is the integer number to represent
366 Ruby's identifiers such as variable names. The Ruby data type
367 corresponding to ID is Symbol. It can be accessed from Ruby in the
372 You can get the ID value from a string within C code by using
374 rb_intern(const char *name)
376 You can retrieve ID from Ruby object (Symbol or String) given as an
379 rb_to_id(VALUE symbol)
381 You can convert C ID to Ruby Symbol by using
385 and to convert Ruby Symbol object to ID, use
387 ID SYM2ID(VALUE symbol)
389 2.2.3 Invoke Ruby method from C
391 To invoke methods directly, you can use the function below
393 VALUE rb_funcall(VALUE recv, ID mid, int argc, ...)
395 This function invokes a method on the recv, with the method name
396 specified by the symbol mid.
398 2.2.4 Accessing the variables and constants
400 You can access class variables and instance variables using access
401 functions. Also, global variables can be shared between both
402 environments. There's no way to access Ruby's local variables.
404 The functions to access/modify instance variables are below:
406 VALUE rb_ivar_get(VALUE obj, ID id)
407 VALUE rb_ivar_set(VALUE obj, ID id, VALUE val)
409 id must be the symbol, which can be retrieved by rb_intern().
411 To access the constants of the class/module:
413 VALUE rb_const_get(VALUE obj, ID id)
415 See 2.1.3 for defining new constant.
417 3. Information sharing between Ruby and C
419 3.1 Ruby constants that C can be accessed from C
421 The following Ruby constants can be referred from C.
426 Boolean values. Qfalse is false in C also (i.e. 0).
432 3.2 Global variables shared between C and Ruby
434 Information can be shared between the two environments using shared global
435 variables. To define them, you can use functions listed below:
437 void rb_define_variable(const char *name, VALUE *var)
439 This function defines the variable which is shared by both environments.
440 The value of the global variable pointed to by `var' can be accessed
441 through Ruby's global variable named `name'.
443 You can define read-only (from Ruby, of course) variables using the
446 void rb_define_readonly_variable(const char *name, VALUE *var)
448 You can defined hooked variables. The accessor functions (getter and
449 setter) are called on access to the hooked variables.
451 void rb_define_hooked_variable(constchar *name, VALUE *var,
452 VALUE (*getter)(), void (*setter)())
454 If you need to supply either setter or getter, just supply 0 for the
455 hook you don't need. If both hooks are 0, rb_define_hooked_variable()
456 works just like rb_define_variable().
458 void rb_define_virtual_variable(const char *name,
459 VALUE (*getter)(), void (*setter)())
461 This function defines a Ruby global variable without a corresponding C
462 variable. The value of the variable will be set/get only by hooks.
464 The prototypes of the getter and setter functions are as follows:
466 (*getter)(ID id, void *data, struct global_entry* entry);
467 (*setter)(VALUE val, ID id, void *data, struct global_entry* entry);
469 3.3 Encapsulate C data into a Ruby object
471 To wrap and objectify a C pointer as a Ruby object (so called
472 DATA), use Data_Wrap_Struct().
474 Data_Wrap_Struct(klass, mark, free, ptr)
476 Data_Wrap_Struct() returns a created DATA object. The klass argument
477 is the class for the DATA object. The mark argument is the function
478 to mark Ruby objects pointed by this data. The free argument is the
479 function to free the pointer allocation. If this is -1, the pointer
480 will be just freed. The functions mark and free will be called from
483 These mark / free functions are invoked during GC execution. No
484 object allocations are allowed during it, so do not allocate ruby
487 You can allocate and wrap the structure in one step.
489 Data_Make_Struct(klass, type, mark, free, sval)
491 This macro returns an allocated Data object, wrapping the pointer to
492 the structure, which is also allocated. This macro works like:
494 (sval = ALLOC(type), Data_Wrap_Struct(klass, mark, free, sval))
496 Arguments klass, mark, and free work like their counterparts in
497 Data_Wrap_Struct(). A pointer to the allocated structure will be
498 assigned to sval, which should be a pointer of the type specified.
500 To retrieve the C pointer from the Data object, use the macro
503 Data_Get_Struct(obj, type, sval)
505 A pointer to the structure will be assigned to the variable sval.
507 See the example below for details.
509 4. Example - Creating dbm extension
511 OK, here's the example of making an extension library. This is the
512 extension to access DBMs. The full source is included in the ext/
513 directory in the Ruby's source tree.
515 (1) make the directory
519 Make a directory for the extension library under ext directory.
521 (2) design the library
523 You need to design the library features, before making it.
527 You need to write C code for your extension library. If your library
528 has only one source file, choosing ``LIBRARY.c'' as a file name is
529 preferred. On the other hand, in case your library has multiple source
530 files, avoid choosing ``LIBRARY.c'' for a file name. It may conflict
531 with an intermediate file ``LIBRARY.o'' on some platforms.
533 Ruby will execute the initializing function named ``Init_LIBRARY'' in
534 the library. For example, ``Init_dbm()'' will be executed when loading
537 Here's the example of an initializing function.
543 /* define DBM class */
544 cDBM = rb_define_class("DBM", rb_cObject);
545 /* DBM includes Enumerate module */
546 rb_include_module(cDBM, rb_mEnumerable);
548 /* DBM has class method open(): arguments are received as C array */
549 rb_define_singleton_method(cDBM, "open", fdbm_s_open, -1);
551 /* DBM instance method close(): no args */
552 rb_define_method(cDBM, "close", fdbm_close, 0);
553 /* DBM instance method []: 1 argument */
554 rb_define_method(cDBM, "[]", fdbm_fetch, 1);
557 /* ID for a instance variable to store DBM data */
558 id_dbm = rb_intern("dbm");
562 The dbm extension wraps the dbm struct in the C environment using
572 obj = Data_Make_Struct(klass, struct dbmdata, 0, free_dbm, dbmp);
575 This code wraps the dbmdata structure into a Ruby object. We avoid
576 wrapping DBM* directly, because we want to cache size information.
578 To retrieve the dbmdata structure from a Ruby object, we define the
582 #define GetDBM(obj, dbmp) {\
583 Data_Get_Struct(obj, struct dbmdata, dbmp);\
584 if (dbmp->di_dbm == 0) closed_dbm();\
588 This sort of complicated macro does the retrieving and close checking for
591 There are three kinds of way to receive method arguments. First,
592 methods with a fixed number of arguments receive arguments like this:
596 fdbm_delete(VALUE obj, VALUE keystr)
602 The first argument of the C function is the self, the rest are the
603 arguments to the method.
605 Second, methods with an arbitrary number of arguments receive
610 fdbm_s_open(int argc, VALUE *argv, VALUE klass)
613 if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
614 mode = 0666; /* default value */
620 The first argument is the number of method arguments, the second
621 argument is the C array of the method arguments, and the third
622 argument is the receiver of the method.
624 You can use the function rb_scan_args() to check and retrieve the
625 arguments. For example, "11" means that the method requires at least one
626 argument, and at most receives two arguments.
628 Methods with an arbitrary number of arguments can receive arguments
629 by Ruby's array, like this:
633 fdbm_indexes(VALUE obj, VALUE args)
639 The first argument is the receiver, the second one is the Ruby array
640 which contains the arguments to the method.
644 GC should know about global variables which refer to Ruby's objects, but
645 are not exported to the Ruby world. You need to protect them by
647 void rb_global_variable(VALUE *var)
649 (4) prepare extconf.rb
651 If the file named extconf.rb exists, it will be executed to generate
654 extconf.rb is the file for checking compilation conditions etc. You
659 at the top of the file. You can use the functions below to check
662 have_library(lib, func): check whether library containing function exists.
663 have_func(func, header): check whether function exists
664 have_header(header): check whether header file exists
665 create_makefile(target): generate Makefile
667 The value of the variables below will affect the Makefile.
669 $CFLAGS: included in CFLAGS make variable (such as -O)
670 $CPPFLAGS: included in CPPFLAGS make variable (such as -I, -D)
671 $LDFLAGS: included in LDFLAGS make variable (such as -L)
672 $objs: list of object file names
674 Normally, the object files list is automatically generated by searching
675 source files, but you must define them explicitly if any sources will
676 be generated while building.
678 If a compilation condition is not fulfilled, you should not call
679 ``create_makefile''. The Makefile will not be generated, compilation will
682 (5) prepare depend (optional)
684 If the file named depend exists, Makefile will include that file to
685 check dependencies. You can make this file by invoking
687 % gcc -MM *.c > depend
689 It's harmless. Prepare it.
691 (6) generate Makefile
693 Try generating the Makefile by:
697 If the library should be installed under vendor_ruby directory
698 instead of site_ruby directory, use --vendor option as follows.
700 ruby extconf.rb --vendor
702 You don't need this step if you put the extension library under the ext
703 directory of the ruby source tree. In that case, compilation of the
704 interpreter will do this step for you.
712 to compile your extension. You don't need this step either if you have
713 put the extension library under the ext directory of the ruby source tree.
717 You may need to rb_debug the extension. Extensions can be linked
718 statically by adding the directory name in the ext/Setup file so that
719 you can inspect the extension with the debugger.
721 (9) done, now you have the extension library
723 You can do anything you want with your library. The author of Ruby
724 will not claim any restrictions on your code depending on the Ruby API.
725 Feel free to use, modify, distribute or sell your program.
727 Appendix A. Ruby source files overview
746 ruby interpreter implementation
779 Appendix B. Ruby extension API reference
785 The type for the Ruby object. Actual structures are defined in ruby.h,
786 such as struct RString, etc. To refer the values in structures, use
787 casting macros like RSTRING(obj).
789 ** Variables and constants
797 const: true object(default true value)
803 ** C pointer wrapping
805 Data_Wrap_Struct(VALUE klass, void (*mark)(), void (*free)(), void *sval)
807 Wrap a C pointer into a Ruby object. If object has references to other
808 Ruby objects, they should be marked by using the mark function during
809 the GC process. Otherwise, mark should be 0. When this object is no
810 longer referred by anywhere, the pointer will be discarded by free
813 Data_Make_Struct(klass, type, mark, free, sval)
815 This macro allocates memory using malloc(), assigns it to the variable
816 sval, and returns the DATA encapsulating the pointer to memory region.
818 Data_Get_Struct(data, type, sval)
820 This macro retrieves the pointer value from DATA, and assigns it to
823 ** Checking data types
828 void Check_Type(VALUE value, int type)
829 void Check_SafeStr(VALUE value)
831 ** Data type conversion
842 StringValuePtr(value)
843 StringValueCStr(value)
846 ** defining class/module
848 VALUE rb_define_class(const char *name, VALUE super)
850 Defines a new Ruby class as a subclass of super.
852 VALUE rb_define_class_under(VALUE module, const char *name, VALUE super)
854 Creates a new Ruby class as a subclass of super, under the module's
857 VALUE rb_define_module(const char *name)
859 Defines a new Ruby module.
861 VALUE rb_define_module_under(VALUE module, const char *name)
863 Defines a new Ruby module under the module's namespace.
865 void rb_include_module(VALUE klass, VALUE module)
867 Includes module into class. If class already includes it, just
870 void rb_extend_object(VALUE object, VALUE module)
872 Extend the object with the module's attributes.
874 ** Defining Global Variables
876 void rb_define_variable(const char *name, VALUE *var)
878 Defines a global variable which is shared between C and Ruby. If name
879 contains a character which is not allowed to be part of the symbol,
880 it can't be seen from Ruby programs.
882 void rb_define_readonly_variable(const char *name, VALUE *var)
884 Defines a read-only global variable. Works just like
885 rb_define_variable(), except the defined variable is read-only.
887 void rb_define_virtual_variable(const char *name,
888 VALUE (*getter)(), VALUE (*setter)())
890 Defines a virtual variable, whose behavior is defined by a pair of C
891 functions. The getter function is called when the variable is
892 referenced. The setter function is called when the variable is set to a
893 value. The prototype for getter/setter functions are:
896 void setter(VALUE val, ID id)
898 The getter function must return the value for the access.
900 void rb_define_hooked_variable(const char *name, VALUE *var,
901 VALUE (*getter)(), VALUE (*setter)())
903 Defines hooked variable. It's a virtual variable with a C variable.
904 The getter is called as
906 VALUE getter(ID id, VALUE *var)
908 returning a new value. The setter is called as
910 void setter(VALUE val, ID id, VALUE *var)
912 GC requires C global variables which hold Ruby values to be marked.
914 void rb_global_variable(VALUE *var)
916 Tells GC to protect these variables.
918 ** Constant Definition
920 void rb_define_const(VALUE klass, const char *name, VALUE val)
922 Defines a new constant under the class/module.
924 void rb_define_global_const(const char *name, VALUE val)
926 Defines a global constant. This is just the same as
928 rb_define_const(cKernal, name, val)
932 rb_define_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
934 Defines a method for the class. func is the function pointer. argc
935 is the number of arguments. if argc is -1, the function will receive
936 3 arguments: argc, argv, and self. if argc is -2, the function will
937 receive 2 arguments, self and args, where args is a Ruby array of
938 the method arguments.
940 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
942 Defines a private method for the class. Arguments are same as
945 rb_define_singleton_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
947 Defines a singleton method. Arguments are same as rb_define_method().
949 rb_scan_args(int argc, VALUE *argv, const char *fmt, ...)
951 Retrieve argument from argc, argv. The fmt is the format string for
952 the arguments, such as "12" for 1 non-optional argument, 2 optional
953 arguments. If `*' appears at the end of fmt, it means the rest of
954 the arguments are assigned to the corresponding variable, packed in
957 ** Invoking Ruby method
959 VALUE rb_funcall(VALUE recv, ID mid, int narg, ...)
961 Invokes a method. To retrieve mid from a method name, use rb_intern().
963 VALUE rb_funcall2(VALUE recv, ID mid, int argc, VALUE *argv)
965 Invokes a method, passing arguments by an array of values.
967 VALUE rb_eval_string(const char *str)
969 Compiles and executes the string as a Ruby program.
971 ID rb_intern(const char *name)
973 Returns ID corresponding to the name.
975 char *rb_id2name(ID id)
977 Returns the name corresponding ID.
979 char *rb_class2name(VALUE klass)
981 Returns the name of the class.
983 int rb_respond_to(VALUE object, ID id)
985 Returns true if the object responds to the message specified by id.
987 ** Instance Variables
989 VALUE rb_iv_get(VALUE obj, const char *name)
991 Retrieve the value of the instance variable. If the name is not
992 prefixed by `@', that variable shall be inaccessible from Ruby.
994 VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
996 Sets the value of the instance variable.
1000 VALUE rb_iterate(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2)
1002 Calls the function func1, supplying func2 as the block. func1 will be
1003 called with the argument arg1. func2 receives the value from yield as
1004 the first argument, arg2 as the second argument.
1006 VALUE rb_yield(VALUE val)
1008 Evaluates the block with value val.
1010 VALUE rb_rescue(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2)
1012 Calls the function func1, with arg1 as the argument. If an exception
1013 occurs during func1, it calls func2 with arg2 as the argument. The
1014 return value of rb_rescue() is the return value from func1 if no
1015 exception occurs, from func2 otherwise.
1017 VALUE rb_ensure(VALUE (*func1)(), void *arg1, void (*func2)(), void *arg2)
1019 Calls the function func1 with arg1 as the argument, then calls func2
1020 with arg2 if execution terminated. The return value from
1021 rb_ensure() is that of func1.
1023 ** Exceptions and Errors
1025 void rb_warn(const char *fmt, ...)
1027 Prints a warning message according to a printf-like format.
1029 void rb_warning(const char *fmt, ...)
1031 Prints a warning message according to a printf-like format, if
1034 void rb_raise(rb_eRuntimeError, const char *fmt, ...)
1036 Raises RuntimeError. The fmt is a format string just like printf().
1038 void rb_raise(VALUE exception, const char *fmt, ...)
1040 Raises a class exception. The fmt is a format string just like printf().
1042 void rb_fatal(const char *fmt, ...)
1044 Raises a fatal error, terminates the interpreter. No exception handling
1045 will be done for fatal errors, but ensure blocks will be executed.
1047 void rb_bug(const char *fmt, ...)
1049 Terminates the interpreter immediately. This function should be
1050 called under the situation caused by the bug in the interpreter. No
1051 exception handling nor ensure execution will be done.
1053 ** Initialize and Start the Interpreter
1055 The embedding API functions are below (not needed for extension libraries):
1059 Initializes the interpreter.
1061 void ruby_options(int argc, char **argv)
1063 Process command line arguments for the interpreter.
1067 Starts execution of the interpreter.
1069 void ruby_script(char *name)
1071 Specifies the name of the script ($0).
1073 ** Hooks for the Interpreter Events
1075 void rb_add_event_hook(rb_event_hook_func_t func, rb_event_t events)
1077 Adds a hook function for the specified interpreter events.
1078 events should be Or'ed value of:
1090 The definition of rb_event_hook_func_t is below:
1092 typedef void (*rb_event_hook_func_t)(rb_event_t event, NODE *node,
1093 VALUE self, ID id, VALUE klass)
1095 int rb_remove_event_hook(rb_event_hook_func_t func)
1097 Removes the specified hook function.
1099 Appendix C. Functions Available in extconf.rb
1101 These functions are available in extconf.rb:
1103 have_macro(macro, headers)
1105 Checks whether macro is defined with header. Returns true if the macro
1108 have_library(lib, func)
1110 Checks whether the library exists, containing the specified function.
1111 Returns true if the library exists.
1113 find_library(lib, func, path...)
1115 Checks whether a library which contains the specified function exists in
1116 path. Returns true if the library exists.
1118 have_func(func, header)
1120 Checks whether func exists with header. Returns true if the function
1121 exists. To check functions in an additional library, you need to
1122 check that library first using have_library().
1124 have_var(var, header)
1126 Checks whether var exists with header. Returns true if the variable
1127 exists. To check variables in an additional library, you need to
1128 check that library first using have_library().
1132 Checks whether header exists. Returns true if the header file exists.
1134 find_header(header, path...)
1136 Checks whether header exists in path. Returns true if the header file
1139 have_struct_member(type, member, header)
1141 Checks whether type has member with header. Returns true if the type
1142 is defined and has the member.
1144 have_type(type, header, opt)
1146 Checks whether type is defined with header. Returns true if the type
1149 check_sizeof(type, header)
1151 Checks the size of type in char with header. Returns the size if the
1152 type is defined, otherwise nil.
1154 create_makefile(target)
1156 Generates the Makefile for the extension library. If you don't invoke
1157 this method, the compilation will not be done.
1159 find_executable(bin, path)
1161 Finds command in path, which is File::PATH_SEPARATOR-separated list of
1162 directories. If path is nil or omitted, environment variable PATH
1163 will be used. Returns the path name of the command if it is found,
1166 with_config(withval[, default=nil])
1168 Parses the command line options and returns the value specified by
1171 enable_config(config, *defaults)
1172 disable_config(config, *defaults)
1174 Parses the command line options for boolean. Returns true if
1175 --enable-<config> is given, or false if --disable-<config> is given.
1176 Otherwise, yields defaults to the given block and returns the result
1177 if it is called with a block, or returns defaults.
1179 dir_config(target[, default_dir])
1180 dir_config(target[, default_include, default_lib])
1182 Parses the command line options and adds the directories specified by
1183 --with-<target>-dir, --with-<target>-include, and/or --with-<target>-lib
1184 to $CFLAGS and/or $LDFLAGS. --with-<target>-dir=/path is equivalent to
1185 --with-<target>-include=/path/include --with-<target>-lib=/path/lib.
1186 Returns an array of the added directories ([include_dir, lib_dir]).
1190 Obtains the information for pkg by pkg-config command. The actual
1191 command name can be overridden by --with-pkg-config command line