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 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. There is also NUM2INT() which converts any Ruby
97 numbers into C integers. The NUM2INT() macro includes a type check,
98 so an exception will be raised if the conversion failed. NUM2DBL()
99 can be used to retrieve the double float value in the same way.
101 In version 1.7 or later it is recommended that you use the new macros
102 StringValue() and StringValuePtr() to get a char* from a VALUE.
103 StringValue(var) replaces var's value with the result of "var.to_str()".
104 StringValuePtr(var) does same replacement and returns char*
105 representation of var. These macros will skip the replacement if var
106 is a String. Notice that the macros take only the lvalue as their
107 argument, to change the value of var in place.
109 In version 1.6 or earlier, STR2CSTR() was used to do the same thing
110 but now it is deprecated in version 1.7, because STR2CSTR() has a risk
111 of a dangling pointer problem in the to_str() implicit conversion.
113 Other data types have corresponding C structures, e.g. struct RArray
114 for T_ARRAY etc. The VALUE of the type which has the corresponding
115 structure can be cast to retrieve the pointer to the struct. The
116 casting macro will be of the form RXXXX for each data type; for
117 instance, RARRAY(obj). See "ruby.h".
119 There are some accessing macros for structure members, for example
120 `RSTRING_LEN(s)' to to get the size of the Ruby String object. The
121 allocated region can be accessed by `RSTRING_PTR(str). For arrays, use
122 `RARRAY_LEN(ary) and `RARRAY_PTR(ary) respectively.
124 Notice: Do not change the value of the structure directly, unless you
125 are responsible for the result. This ends up being the cause of
128 1.4 Convert C data into VALUE
130 To convert C data to Ruby values:
134 left shift 1 bit, and turn on LSB.
136 * Other pointer values
140 You can determine whether a VALUE is pointer or not by checking its LSB.
142 Notice Ruby does not allow arbitrary pointer values to be a VALUE. They
143 should be pointers to the structures which Ruby knows about. The known
144 structures are defined in <ruby.h>.
146 To convert C numbers to Ruby values, use these macros.
148 INT2FIX() for integers within 31bits.
149 INT2NUM() for arbitrary sized integer.
151 INT2NUM() converts an integer into a Bignum if it is out of the FIXNUM
152 range, but is a bit slower.
154 1.5 Manipulating Ruby data
156 As I already mentioned, it is not recommended to modify an object's
157 internal structure. To manipulate objects, use the functions supplied
158 by the Ruby interpreter. Some (not all) of the useful functions are
163 rb_str_new(const char *ptr, long len)
165 Creates a new Ruby string.
167 rb_str_new2(const char *ptr)
169 Creates a new Ruby string from a C string. This is equivalent to
170 rb_str_new(ptr, strlen(ptr)).
172 rb_tainted_str_new(const char *ptr, long len)
174 Creates a new tainted Ruby string. Strings from external data
175 sources should be tainted.
177 rb_tainted_str_new2(const char *ptr)
179 Creates a new tainted Ruby string from a C string.
181 rb_str_cat(VALUE str, const char *ptr, long len)
183 Appends len bytes of data from ptr to the Ruby string.
189 Creates an array with no elements.
191 rb_ary_new2(long len)
193 Creates an array with no elements, allocating internal buffer
196 rb_ary_new3(long n, ...)
198 Creates an n-element array from the arguments.
200 rb_ary_new4(long n, VALUE *elts)
202 Creates an n-element array from a C array.
204 rb_ary_push(VALUE ary, VALUE val)
205 rb_ary_pop(VALUE ary)
206 rb_ary_shift(VALUE ary)
207 rb_ary_unshift(VALUE ary, VALUE val)
209 Array operations. The first argument to each functions must be an
210 array. They may dump core if other types are given.
212 2. Extending Ruby with C
214 2.1 Adding new features to Ruby
216 You can add new features (classes, methods, etc.) to the Ruby
217 interpreter. Ruby provides APIs for defining the following things:
220 * Methods, Singleton Methods
223 2.1.1 Class/module definition
225 To define a class or module, use the functions below:
227 VALUE rb_define_class(const char *name, VALUE super)
228 VALUE rb_define_module(const char *name)
230 These functions return the newly created class or module. You may
231 want to save this reference into a variable to use later.
233 To define nested classes or modules, use the functions below:
235 VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
236 VALUE rb_define_module_under(VALUE outer, const char *name)
238 2.1.2 Method/singleton method definition
240 To define methods or singleton methods, use these functions:
242 void rb_define_method(VALUE klass, const char *name,
243 VALUE (*func)(), int argc)
245 void rb_define_singleton_method(VALUE object, const char *name,
246 VALUE (*func)(), int argc)
248 The `argc' represents the number of the arguments to the C function,
249 which must be less than 17. But I doubt you'll need that many.
251 If `argc' is negative, it specifies the calling sequence, not number of
254 If argc is -1, the function will be called as:
256 VALUE func(int argc, VALUE *argv, VALUE obj)
258 where argc is the actual number of arguments, argv is the C array of
259 the arguments, and obj is the receiver.
261 If argc is -2, the arguments are passed in a Ruby array. The function
264 VALUE func(VALUE obj, VALUE args)
266 where obj is the receiver, and args is the Ruby array containing
269 There are two more functions to define methods. One is to define
272 void rb_define_private_method(VALUE klass, const char *name,
273 VALUE (*func)(), int argc)
275 The other is to define module functions, which are private AND singleton
276 methods of the module. For example, sqrt is the module function
277 defined in Math module. It can be called in the following way:
286 To define module functions, use:
288 void rb_define_module_function(VALUE module, const char *name,
289 VALUE (*func)(), int argc)
291 Oh, in addition, function-like methods, which are private methods defined
292 in the Kernel module, can be defined using:
294 void rb_define_global_function(const char *name, VALUE (*func)(), int argc)
296 To define an alias for the method,
298 void rb_define_alias(VALUE module, const char* new, const char* old);
300 To define and undefine the `allocate' class method,
302 void rb_define_alloc_func(VALUE klass, VALUE (*func)(VALUE klass));
303 void rb_undef_alloc_func(VALUE klass);
305 func have to take the klass as the argument and return a newly
306 allocated instance. This instance should be empty as possible,
307 without any expensive (including external) resources.
309 2.1.3 Constant definition
311 We have 2 functions to define constants:
313 void rb_define_const(VALUE klass, const char *name, VALUE val)
314 void rb_define_global_const(const char *name, VALUE val)
316 The former is to define a constant under specified class/module. The
317 latter is to define a global constant.
319 2.2 Use Ruby features from C
321 There are several ways to invoke Ruby's features from C code.
323 2.2.1 Evaluate Ruby Programs in a String
325 The easiest way to use Ruby's functionality from a C program is to
326 evaluate the string as Ruby program. This function will do the job:
328 VALUE rb_eval_string(const char *str)
330 Evaluation is done under the current context, thus current local variables
331 of the innermost method (which is defined by Ruby) can be accessed.
335 You can invoke methods directly, without parsing the string. First I
336 need to explain about ID. ID is the integer number to represent
337 Ruby's identifiers such as variable names. The Ruby data type
338 corresponding to ID is Symbol. It can be accessed from Ruby in the
343 You can get the ID value from a string within C code by using
345 rb_intern(const char *name)
347 You can retrieve ID from Ruby object (Symbol or String) given as an
350 rb_to_id(VALUE symbol)
352 You can convert C ID to Ruby Symbol by using
356 and to convert Ruby Symbol object to ID, use
358 ID SYM2ID(VALUE symbol)
360 2.2.3 Invoke Ruby method from C
362 To invoke methods directly, you can use the function below
364 VALUE rb_funcall(VALUE recv, ID mid, int argc, ...)
366 This function invokes a method on the recv, with the method name
367 specified by the symbol mid.
369 2.2.4 Accessing the variables and constants
371 You can access class variables and instance variables using access
372 functions. Also, global variables can be shared between both
373 environments. There's no way to access Ruby's local variables.
375 The functions to access/modify instance variables are below:
377 VALUE rb_ivar_get(VALUE obj, ID id)
378 VALUE rb_ivar_set(VALUE obj, ID id, VALUE val)
380 id must be the symbol, which can be retrieved by rb_intern().
382 To access the constants of the class/module:
384 VALUE rb_const_get(VALUE obj, ID id)
386 See 2.1.3 for defining new constant.
388 3. Information sharing between Ruby and C
390 3.1 Ruby constants that C can be accessed from C
392 The following Ruby constants can be referred from C.
397 Boolean values. Qfalse is false in C also (i.e. 0).
403 3.2 Global variables shared between C and Ruby
405 Information can be shared between the two environments using shared global
406 variables. To define them, you can use functions listed below:
408 void rb_define_variable(const char *name, VALUE *var)
410 This function defines the variable which is shared by both environments.
411 The value of the global variable pointed to by `var' can be accessed
412 through Ruby's global variable named `name'.
414 You can define read-only (from Ruby, of course) variables using the
417 void rb_define_readonly_variable(const char *name, VALUE *var)
419 You can defined hooked variables. The accessor functions (getter and
420 setter) are called on access to the hooked variables.
422 void rb_define_hooked_variable(constchar *name, VALUE *var,
423 VALUE (*getter)(), void (*setter)())
425 If you need to supply either setter or getter, just supply 0 for the
426 hook you don't need. If both hooks are 0, rb_define_hooked_variable()
427 works just like rb_define_variable().
429 void rb_define_virtual_variable(const char *name,
430 VALUE (*getter)(), void (*setter)())
432 This function defines a Ruby global variable without a corresponding C
433 variable. The value of the variable will be set/get only by hooks.
435 The prototypes of the getter and setter functions are as follows:
437 (*getter)(ID id, void *data, struct global_entry* entry);
438 (*setter)(VALUE val, ID id, void *data, struct global_entry* entry);
440 3.3 Encapsulate C data into a Ruby object
442 To wrap and objectify a C pointer as a Ruby object (so called
443 DATA), use Data_Wrap_Struct().
445 Data_Wrap_Struct(klass, mark, free, ptr)
447 Data_Wrap_Struct() returns a created DATA object. The klass argument
448 is the class for the DATA object. The mark argument is the function
449 to mark Ruby objects pointed by this data. The free argument is the
450 function to free the pointer allocation. If this is -1, the pointer
451 will be just freed. The functions mark and free will be called from
454 You can allocate and wrap the structure in one step.
456 Data_Make_Struct(klass, type, mark, free, sval)
458 This macro returns an allocated Data object, wrapping the pointer to
459 the structure, which is also allocated. This macro works like:
461 (sval = ALLOC(type), Data_Wrap_Struct(klass, mark, free, sval))
463 Arguments klass, mark, and free work like their counterparts in
464 Data_Wrap_Struct(). A pointer to the allocated structure will be
465 assigned to sval, which should be a pointer of the type specified.
467 To retrieve the C pointer from the Data object, use the macro
470 Data_Get_Struct(obj, type, sval)
472 A pointer to the structure will be assigned to the variable sval.
474 See the example below for details.
476 4. Example - Creating dbm extension
478 OK, here's the example of making an extension library. This is the
479 extension to access DBMs. The full source is included in the ext/
480 directory in the Ruby's source tree.
482 (1) make the directory
486 Make a directory for the extension library under ext directory.
488 (2) design the library
490 You need to design the library features, before making it.
494 You need to write C code for your extension library. If your library
495 has only one source file, choosing ``LIBRARY.c'' as a file name is
496 preferred. On the other hand, in case your library has multiple source
497 files, avoid choosing ``LIBRARY.c'' for a file name. It may conflict
498 with an intermediate file ``LIBRARY.o'' on some platforms.
500 Ruby will execute the initializing function named ``Init_LIBRARY'' in
501 the library. For example, ``Init_dbm()'' will be executed when loading
504 Here's the example of an initializing function.
510 /* define DBM class */
511 cDBM = rb_define_class("DBM", rb_cObject);
512 /* DBM includes Enumerate module */
513 rb_include_module(cDBM, rb_mEnumerable);
515 /* DBM has class method open(): arguments are received as C array */
516 rb_define_singleton_method(cDBM, "open", fdbm_s_open, -1);
518 /* DBM instance method close(): no args */
519 rb_define_method(cDBM, "close", fdbm_close, 0);
520 /* DBM instance method []: 1 argument */
521 rb_define_method(cDBM, "[]", fdbm_fetch, 1);
524 /* ID for a instance variable to store DBM data */
525 id_dbm = rb_intern("dbm");
529 The dbm extension wraps the dbm struct in the C environment using
539 obj = Data_Make_Struct(klass, struct dbmdata, 0, free_dbm, dbmp);
542 This code wraps the dbmdata structure into a Ruby object. We avoid
543 wrapping DBM* directly, because we want to cache size information.
545 To retrieve the dbmdata structure from a Ruby object, we define the
549 #define GetDBM(obj, dbmp) {\
550 Data_Get_Struct(obj, struct dbmdata, dbmp);\
551 if (dbmp->di_dbm == 0) closed_dbm();\
555 This sort of complicated macro does the retrieving and close checking for
558 There are three kinds of way to receive method arguments. First,
559 methods with a fixed number of arguments receive arguments like this:
563 fdbm_delete(VALUE obj, VALUE keystr)
569 The first argument of the C function is the self, the rest are the
570 arguments to the method.
572 Second, methods with an arbitrary number of arguments receive
577 fdbm_s_open(int argc, VALUE *argv, VALUE klass)
580 if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
581 mode = 0666; /* default value */
587 The first argument is the number of method arguments, the second
588 argument is the C array of the method arguments, and the third
589 argument is the receiver of the method.
591 You can use the function rb_scan_args() to check and retrieve the
592 arguments. For example, "11" means that the method requires at least one
593 argument, and at most receives two arguments.
595 Methods with an arbitrary number of arguments can receive arguments
596 by Ruby's array, like this:
600 fdbm_indexes(VALUE obj, VALUE args)
606 The first argument is the receiver, the second one is the Ruby array
607 which contains the arguments to the method.
611 GC should know about global variables which refer to Ruby's objects, but
612 are not exported to the Ruby world. You need to protect them by
614 void rb_global_variable(VALUE *var)
616 (4) prepare extconf.rb
618 If the file named extconf.rb exists, it will be executed to generate
621 extconf.rb is the file for checking compilation conditions etc. You
626 at the top of the file. You can use the functions below to check
629 have_library(lib, func): check whether library containing function exists.
630 have_func(func, header): check whether function exists
631 have_header(header): check whether header file exists
632 create_makefile(target): generate Makefile
634 The value of the variables below will affect the Makefile.
636 $CFLAGS: included in CFLAGS make variable (such as -O)
637 $CPPFLAGS: included in CPPFLAGS make variable (such as -I, -D)
638 $LDFLAGS: included in LDFLAGS make variable (such as -L)
639 $objs: list of object file names
641 Normally, the object files list is automatically generated by searching
642 source files, but you must define them explicitly if any sources will
643 be generated while building.
645 If a compilation condition is not fulfilled, you should not call
646 ``create_makefile''. The Makefile will not be generated, compilation will
649 (5) prepare depend (optional)
651 If the file named depend exists, Makefile will include that file to
652 check dependencies. You can make this file by invoking
654 % gcc -MM *.c > depend
656 It's harmless. Prepare it.
658 (6) generate Makefile
660 Try generating the Makefile by:
664 If the library should be installed under vendor_ruby directory
665 instead of site_ruby directory, use --vendor option as follows.
667 ruby extconf.rb --vendor
669 You don't need this step if you put the extension library under the ext
670 directory of the ruby source tree. In that case, compilation of the
671 interpreter will do this step for you.
679 to compile your extension. You don't need this step either if you have
680 put the extension library under the ext directory of the ruby source tree.
684 You may need to rb_debug the extension. Extensions can be linked
685 statically by adding the directory name in the ext/Setup file so that
686 you can inspect the extension with the debugger.
688 (9) done, now you have the extension library
690 You can do anything you want with your library. The author of Ruby
691 will not claim any restrictions on your code depending on the Ruby API.
692 Feel free to use, modify, distribute or sell your program.
694 Appendix A. Ruby source files overview
713 ruby interpreter implementation
746 Appendix B. Ruby extension API reference
752 The type for the Ruby object. Actual structures are defined in ruby.h,
753 such as struct RString, etc. To refer the values in structures, use
754 casting macros like RSTRING(obj).
756 ** Variables and constants
764 const: true object(default true value)
770 ** C pointer wrapping
772 Data_Wrap_Struct(VALUE klass, void (*mark)(), void (*free)(), void *sval)
774 Wrap a C pointer into a Ruby object. If object has references to other
775 Ruby objects, they should be marked by using the mark function during
776 the GC process. Otherwise, mark should be 0. When this object is no
777 longer referred by anywhere, the pointer will be discarded by free
780 Data_Make_Struct(klass, type, mark, free, sval)
782 This macro allocates memory using malloc(), assigns it to the variable
783 sval, and returns the DATA encapsulating the pointer to memory region.
785 Data_Get_Struct(data, type, sval)
787 This macro retrieves the pointer value from DATA, and assigns it to
790 ** Checking data types
795 void Check_Type(VALUE value, int type)
796 void Check_SafeStr(VALUE value)
798 ** Data type conversion
807 StringValuePtr(value)
808 StringValueCStr(value)
811 ** defining class/module
813 VALUE rb_define_class(const char *name, VALUE super)
815 Defines a new Ruby class as a subclass of super.
817 VALUE rb_define_class_under(VALUE module, const char *name, VALUE super)
819 Creates a new Ruby class as a subclass of super, under the module's
822 VALUE rb_define_module(const char *name)
824 Defines a new Ruby module.
826 VALUE rb_define_module_under(VALUE module, const char *name)
828 Defines a new Ruby module under the module's namespace.
830 void rb_include_module(VALUE klass, VALUE module)
832 Includes module into class. If class already includes it, just
835 void rb_extend_object(VALUE object, VALUE module)
837 Extend the object with the module's attributes.
839 ** Defining Global Variables
841 void rb_define_variable(const char *name, VALUE *var)
843 Defines a global variable which is shared between C and Ruby. If name
844 contains a character which is not allowed to be part of the symbol,
845 it can't be seen from Ruby programs.
847 void rb_define_readonly_variable(const char *name, VALUE *var)
849 Defines a read-only global variable. Works just like
850 rb_define_variable(), except the defined variable is read-only.
852 void rb_define_virtual_variable(const char *name,
853 VALUE (*getter)(), VALUE (*setter)())
855 Defines a virtual variable, whose behavior is defined by a pair of C
856 functions. The getter function is called when the variable is
857 referenced. The setter function is called when the variable is set to a
858 value. The prototype for getter/setter functions are:
861 void setter(VALUE val, ID id)
863 The getter function must return the value for the access.
865 void rb_define_hooked_variable(const char *name, VALUE *var,
866 VALUE (*getter)(), VALUE (*setter)())
868 Defines hooked variable. It's a virtual variable with a C variable.
869 The getter is called as
871 VALUE getter(ID id, VALUE *var)
873 returning a new value. The setter is called as
875 void setter(VALUE val, ID id, VALUE *var)
877 GC requires C global variables which hold Ruby values to be marked.
879 void rb_global_variable(VALUE *var)
881 Tells GC to protect these variables.
883 ** Constant Definition
885 void rb_define_const(VALUE klass, const char *name, VALUE val)
887 Defines a new constant under the class/module.
889 void rb_define_global_const(const char *name, VALUE val)
891 Defines a global constant. This is just the same as
893 rb_define_const(cKernal, name, val)
897 rb_define_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
899 Defines a method for the class. func is the function pointer. argc
900 is the number of arguments. if argc is -1, the function will receive
901 3 arguments: argc, argv, and self. if argc is -2, the function will
902 receive 2 arguments, self and args, where args is a Ruby array of
903 the method arguments.
905 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
907 Defines a private method for the class. Arguments are same as
910 rb_define_singleton_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
912 Defines a singleton method. Arguments are same as rb_define_method().
914 rb_scan_args(int argc, VALUE *argv, const char *fmt, ...)
916 Retrieve argument from argc, argv. The fmt is the format string for
917 the arguments, such as "12" for 1 non-optional argument, 2 optional
918 arguments. If `*' appears at the end of fmt, it means the rest of
919 the arguments are assigned to the corresponding variable, packed in
922 ** Invoking Ruby method
924 VALUE rb_funcall(VALUE recv, ID mid, int narg, ...)
926 Invokes a method. To retrieve mid from a method name, use rb_intern().
928 VALUE rb_funcall2(VALUE recv, ID mid, int argc, VALUE *argv)
930 Invokes a method, passing arguments by an array of values.
932 VALUE rb_eval_string(const char *str)
934 Compiles and executes the string as a Ruby program.
936 ID rb_intern(const char *name)
938 Returns ID corresponding to the name.
940 char *rb_id2name(ID id)
942 Returns the name corresponding ID.
944 char *rb_class2name(VALUE klass)
946 Returns the name of the class.
948 int rb_respond_to(VALUE object, ID id)
950 Returns true if the object responds to the message specified by id.
952 ** Instance Variables
954 VALUE rb_iv_get(VALUE obj, const char *name)
956 Retrieve the value of the instance variable. If the name is not
957 prefixed by `@', that variable shall be inaccessible from Ruby.
959 VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
961 Sets the value of the instance variable.
965 VALUE rb_iterate(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2)
967 Calls the function func1, supplying func2 as the block. func1 will be
968 called with the argument arg1. func2 receives the value from yield as
969 the first argument, arg2 as the second argument.
971 VALUE rb_yield(VALUE val)
973 Evaluates the block with value val.
975 VALUE rb_rescue(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2)
977 Calls the function func1, with arg1 as the argument. If an exception
978 occurs during func1, it calls func2 with arg2 as the argument. The
979 return value of rb_rescue() is the return value from func1 if no
980 exception occurs, from func2 otherwise.
982 VALUE rb_ensure(VALUE (*func1)(), void *arg1, void (*func2)(), void *arg2)
984 Calls the function func1 with arg1 as the argument, then calls func2
985 with arg2 if execution terminated. The return value from
986 rb_ensure() is that of func1.
988 ** Exceptions and Errors
990 void rb_warn(const char *fmt, ...)
992 Prints a warning message according to a printf-like format.
994 void rb_warning(const char *fmt, ...)
996 Prints a warning message according to a printf-like format, if
999 void rb_raise(rb_eRuntimeError, const char *fmt, ...)
1001 Raises RuntimeError. The fmt is a format string just like printf().
1003 void rb_raise(VALUE exception, const char *fmt, ...)
1005 Raises a class exception. The fmt is a format string just like printf().
1007 void rb_fatal(const char *fmt, ...)
1009 Raises a fatal error, terminates the interpreter. No exception handling
1010 will be done for fatal errors, but ensure blocks will be executed.
1012 void rb_bug(const char *fmt, ...)
1014 Terminates the interpreter immediately. This function should be
1015 called under the situation caused by the bug in the interpreter. No
1016 exception handling nor ensure execution will be done.
1018 ** Initialize and Start the Interpreter
1020 The embedding API functions are below (not needed for extension libraries):
1024 Initializes the interpreter.
1026 void ruby_options(int argc, char **argv)
1028 Process command line arguments for the interpreter.
1032 Starts execution of the interpreter.
1034 void ruby_script(char *name)
1036 Specifies the name of the script ($0).
1038 ** Hooks for the Interpreter Events
1040 void rb_add_event_hook(rb_event_hook_func_t func, rb_event_t events)
1042 Adds a hook function for the specified interpreter events.
1043 events should be Or'ed value of:
1055 The definition of rb_event_hook_func_t is below:
1057 typedef void (*rb_event_hook_func_t)(rb_event_t event, NODE *node,
1058 VALUE self, ID id, VALUE klass)
1060 int rb_remove_event_hook(rb_event_hook_func_t func)
1062 Removes the specified hook function.
1064 Appendix C. Functions Available in extconf.rb
1066 These functions are available in extconf.rb:
1068 have_macro(macro, headers)
1070 Checks whether macro is defined with header. Returns true if the macro
1073 have_library(lib, func)
1075 Checks whether the library exists, containing the specified function.
1076 Returns true if the library exists.
1078 find_library(lib, func, path...)
1080 Checks whether a library which contains the specified function exists in
1081 path. Returns true if the library exists.
1083 have_func(func, header)
1085 Checks whether func exists with header. Returns true if the function
1086 exists. To check functions in an additional library, you need to
1087 check that library first using have_library().
1089 have_var(var, header)
1091 Checks whether var exists with header. Returns true if the variable
1092 exists. To check variables in an additional library, you need to
1093 check that library first using have_library().
1097 Checks whether header exists. Returns true if the header file exists.
1099 find_header(header, path...)
1101 Checks whether header exists in path. Returns true if the header file
1104 have_struct_member(type, member, header)
1106 Checks whether type has member with header. Returns true if the type
1107 is defined and has the member.
1109 have_type(type, header, opt)
1111 Checks whether type is defined with header. Returns true if the type
1114 check_sizeof(type, header)
1116 Checks the size of type in char with header. Returns the size if the
1117 type is defined, otherwise nil.
1119 create_makefile(target)
1121 Generates the Makefile for the extension library. If you don't invoke
1122 this method, the compilation will not be done.
1124 find_executable(bin, path)
1126 Finds command in path, which is File::PATH_SEPARATOR-separated list of
1127 directories. If path is nil or omitted, environment variable PATH
1128 will be used. Returns the path name of the command if it is found,
1131 with_config(withval[, default=nil])
1133 Parses the command line options and returns the value specified by
1136 enable_config(config, *defaults)
1137 disable_config(config, *defaults)
1139 Parses the command line options for boolean. Returns true if
1140 --enable-<config> is given, or false if --disable-<config> is given.
1141 Otherwise, yields defaults to the given block and returns the result
1142 if it is called with a block, or returns defaults.
1144 dir_config(target[, default_dir])
1145 dir_config(target[, default_include, default_lib])
1147 Parses the command line options and adds the directories specified by
1148 --with-<target>-dir, --with-<target>-include, and/or --with-<target>-lib
1149 to $CFLAGS and/or $LDFLAGS. --with-<target>-dir=/path is equivalent to
1150 --with-<target>-include=/path/include --with-<target>-lib=/path/lib.
1151 Returns an array of the added directories ([include_dir, lib_dir]).
1155 Obtains the information for pkg by pkg-config command. The actual
1156 command name can be overridden by --with-pkg-config command line