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. 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 You can also use the macro named StringValueCStr(). This is just
110 like StringValuePtr(), but always add nul character at the end of
111 the result. If the result contains nul character, this macro causes
112 the ArgumentError exception.
113 StringValuePtr() doesn't gurantee to exist nul at the end of the
114 result, and the result may contain nul.
116 In version 1.6 or earlier, STR2CSTR() was used to do the same thing
117 but now it is deprecated in version 1.7, because STR2CSTR() has a risk
118 of a dangling pointer problem in the to_str() implicit conversion.
120 Other data types have corresponding C structures, e.g. struct RArray
121 for T_ARRAY etc. The VALUE of the type which has the corresponding
122 structure can be cast to retrieve the pointer to the struct. The
123 casting macro will be of the form RXXXX for each data type; for
124 instance, RARRAY(obj). See "ruby.h".
126 There are some accessing macros for structure members, for example
127 `RSTRING_LEN(s)' to to get the size of the Ruby String object. The
128 allocated region can be accessed by `RSTRING_PTR(str). For arrays, use
129 `RARRAY_LEN(ary) and `RARRAY_PTR(ary) respectively.
131 Notice: Do not change the value of the structure directly, unless you
132 are responsible for the result. This ends up being the cause of
135 1.4 Convert C data into VALUE
137 To convert C data to Ruby values:
141 left shift 1 bit, and turn on LSB.
143 * Other pointer values
147 You can determine whether a VALUE is pointer or not by checking its LSB.
149 Notice Ruby does not allow arbitrary pointer values to be a VALUE. They
150 should be pointers to the structures which Ruby knows about. The known
151 structures are defined in <ruby.h>.
153 To convert C numbers to Ruby values, use these macros.
155 INT2FIX() for integers within 31bits.
156 INT2NUM() for arbitrary sized integer.
158 INT2NUM() converts an integer into a Bignum if it is out of the FIXNUM
159 range, but is a bit slower.
161 1.5 Manipulating Ruby data
163 As I already mentioned, it is not recommended to modify an object's
164 internal structure. To manipulate objects, use the functions supplied
165 by the Ruby interpreter. Some (not all) of the useful functions are
170 rb_str_new(const char *ptr, long len)
172 Creates a new Ruby string.
174 rb_str_new2(const char *ptr)
176 Creates a new Ruby string from a C string. This is equivalent to
177 rb_str_new(ptr, strlen(ptr)).
179 rb_tainted_str_new(const char *ptr, long len)
181 Creates a new tainted Ruby string. Strings from external data
182 sources should be tainted.
184 rb_tainted_str_new2(const char *ptr)
186 Creates a new tainted Ruby string from a C string.
188 rb_str_cat(VALUE str, const char *ptr, long len)
190 Appends len bytes of data from ptr to the Ruby string.
196 Creates an array with no elements.
198 rb_ary_new2(long len)
200 Creates an array with no elements, allocating internal buffer
203 rb_ary_new3(long n, ...)
205 Creates an n-element array from the arguments.
207 rb_ary_new4(long n, VALUE *elts)
209 Creates an n-element array from a C array.
211 rb_ary_push(VALUE ary, VALUE val)
212 rb_ary_pop(VALUE ary)
213 rb_ary_shift(VALUE ary)
214 rb_ary_unshift(VALUE ary, VALUE val)
216 Array operations. The first argument to each functions must be an
217 array. They may dump core if other types are given.
219 2. Extending Ruby with C
221 2.1 Adding new features to Ruby
223 You can add new features (classes, methods, etc.) to the Ruby
224 interpreter. Ruby provides APIs for defining the following things:
227 * Methods, Singleton Methods
230 2.1.1 Class/module definition
232 To define a class or module, use the functions below:
234 VALUE rb_define_class(const char *name, VALUE super)
235 VALUE rb_define_module(const char *name)
237 These functions return the newly created class or module. You may
238 want to save this reference into a variable to use later.
240 To define nested classes or modules, use the functions below:
242 VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
243 VALUE rb_define_module_under(VALUE outer, const char *name)
245 2.1.2 Method/singleton method definition
247 To define methods or singleton methods, use these functions:
249 void rb_define_method(VALUE klass, const char *name,
250 VALUE (*func)(), int argc)
252 void rb_define_singleton_method(VALUE object, const char *name,
253 VALUE (*func)(), int argc)
255 The `argc' represents the number of the arguments to the C function,
256 which must be less than 17. But I doubt you'll need that many.
258 If `argc' is negative, it specifies the calling sequence, not number of
261 If argc is -1, the function will be called as:
263 VALUE func(int argc, VALUE *argv, VALUE obj)
265 where argc is the actual number of arguments, argv is the C array of
266 the arguments, and obj is the receiver.
268 If argc is -2, the arguments are passed in a Ruby array. The function
271 VALUE func(VALUE obj, VALUE args)
273 where obj is the receiver, and args is the Ruby array containing
276 There are two more functions to define methods. One is to define
279 void rb_define_private_method(VALUE klass, const char *name,
280 VALUE (*func)(), int argc)
282 The other is to define module functions, which are private AND singleton
283 methods of the module. For example, sqrt is the module function
284 defined in Math module. It can be called in the following way:
293 To define module functions, use:
295 void rb_define_module_function(VALUE module, const char *name,
296 VALUE (*func)(), int argc)
298 Oh, in addition, function-like methods, which are private methods defined
299 in the Kernel module, can be defined using:
301 void rb_define_global_function(const char *name, VALUE (*func)(), int argc)
303 To define an alias for the method,
305 void rb_define_alias(VALUE module, const char* new, const char* old);
307 To define and undefine the `allocate' class method,
309 void rb_define_alloc_func(VALUE klass, VALUE (*func)(VALUE klass));
310 void rb_undef_alloc_func(VALUE klass);
312 func have to take the klass as the argument and return a newly
313 allocated instance. This instance should be empty as possible,
314 without any expensive (including external) resources.
316 2.1.3 Constant definition
318 We have 2 functions to define constants:
320 void rb_define_const(VALUE klass, const char *name, VALUE val)
321 void rb_define_global_const(const char *name, VALUE val)
323 The former is to define a constant under specified class/module. The
324 latter is to define a global constant.
326 2.2 Use Ruby features from C
328 There are several ways to invoke Ruby's features from C code.
330 2.2.1 Evaluate Ruby Programs in a String
332 The easiest way to use Ruby's functionality from a C program is to
333 evaluate the string as Ruby program. This function will do the job:
335 VALUE rb_eval_string(const char *str)
337 Evaluation is done under the current context, thus current local variables
338 of the innermost method (which is defined by Ruby) can be accessed.
342 You can invoke methods directly, without parsing the string. First I
343 need to explain about ID. ID is the integer number to represent
344 Ruby's identifiers such as variable names. The Ruby data type
345 corresponding to ID is Symbol. It can be accessed from Ruby in the
350 You can get the ID value from a string within C code by using
352 rb_intern(const char *name)
354 You can retrieve ID from Ruby object (Symbol or String) given as an
357 rb_to_id(VALUE symbol)
359 You can convert C ID to Ruby Symbol by using
363 and to convert Ruby Symbol object to ID, use
365 ID SYM2ID(VALUE symbol)
367 2.2.3 Invoke Ruby method from C
369 To invoke methods directly, you can use the function below
371 VALUE rb_funcall(VALUE recv, ID mid, int argc, ...)
373 This function invokes a method on the recv, with the method name
374 specified by the symbol mid.
376 2.2.4 Accessing the variables and constants
378 You can access class variables and instance variables using access
379 functions. Also, global variables can be shared between both
380 environments. There's no way to access Ruby's local variables.
382 The functions to access/modify instance variables are below:
384 VALUE rb_ivar_get(VALUE obj, ID id)
385 VALUE rb_ivar_set(VALUE obj, ID id, VALUE val)
387 id must be the symbol, which can be retrieved by rb_intern().
389 To access the constants of the class/module:
391 VALUE rb_const_get(VALUE obj, ID id)
393 See 2.1.3 for defining new constant.
395 3. Information sharing between Ruby and C
397 3.1 Ruby constants that C can be accessed from C
399 The following Ruby constants can be referred from C.
404 Boolean values. Qfalse is false in C also (i.e. 0).
410 3.2 Global variables shared between C and Ruby
412 Information can be shared between the two environments using shared global
413 variables. To define them, you can use functions listed below:
415 void rb_define_variable(const char *name, VALUE *var)
417 This function defines the variable which is shared by both environments.
418 The value of the global variable pointed to by `var' can be accessed
419 through Ruby's global variable named `name'.
421 You can define read-only (from Ruby, of course) variables using the
424 void rb_define_readonly_variable(const char *name, VALUE *var)
426 You can defined hooked variables. The accessor functions (getter and
427 setter) are called on access to the hooked variables.
429 void rb_define_hooked_variable(constchar *name, VALUE *var,
430 VALUE (*getter)(), void (*setter)())
432 If you need to supply either setter or getter, just supply 0 for the
433 hook you don't need. If both hooks are 0, rb_define_hooked_variable()
434 works just like rb_define_variable().
436 void rb_define_virtual_variable(const char *name,
437 VALUE (*getter)(), void (*setter)())
439 This function defines a Ruby global variable without a corresponding C
440 variable. The value of the variable will be set/get only by hooks.
442 The prototypes of the getter and setter functions are as follows:
444 (*getter)(ID id, void *data, struct global_entry* entry);
445 (*setter)(VALUE val, ID id, void *data, struct global_entry* entry);
447 3.3 Encapsulate C data into a Ruby object
449 To wrap and objectify a C pointer as a Ruby object (so called
450 DATA), use Data_Wrap_Struct().
452 Data_Wrap_Struct(klass, mark, free, ptr)
454 Data_Wrap_Struct() returns a created DATA object. The klass argument
455 is the class for the DATA object. The mark argument is the function
456 to mark Ruby objects pointed by this data. The free argument is the
457 function to free the pointer allocation. If this is -1, the pointer
458 will be just freed. The functions mark and free will be called from
461 These mark / free functions are invoked during GC execution. No
462 object allocations are allowed during it, so do not allocate ruby
465 You can allocate and wrap the structure in one step.
467 Data_Make_Struct(klass, type, mark, free, sval)
469 This macro returns an allocated Data object, wrapping the pointer to
470 the structure, which is also allocated. This macro works like:
472 (sval = ALLOC(type), Data_Wrap_Struct(klass, mark, free, sval))
474 Arguments klass, mark, and free work like their counterparts in
475 Data_Wrap_Struct(). A pointer to the allocated structure will be
476 assigned to sval, which should be a pointer of the type specified.
478 To retrieve the C pointer from the Data object, use the macro
481 Data_Get_Struct(obj, type, sval)
483 A pointer to the structure will be assigned to the variable sval.
485 See the example below for details.
487 4. Example - Creating dbm extension
489 OK, here's the example of making an extension library. This is the
490 extension to access DBMs. The full source is included in the ext/
491 directory in the Ruby's source tree.
493 (1) make the directory
497 Make a directory for the extension library under ext directory.
499 (2) design the library
501 You need to design the library features, before making it.
505 You need to write C code for your extension library. If your library
506 has only one source file, choosing ``LIBRARY.c'' as a file name is
507 preferred. On the other hand, in case your library has multiple source
508 files, avoid choosing ``LIBRARY.c'' for a file name. It may conflict
509 with an intermediate file ``LIBRARY.o'' on some platforms.
511 Ruby will execute the initializing function named ``Init_LIBRARY'' in
512 the library. For example, ``Init_dbm()'' will be executed when loading
515 Here's the example of an initializing function.
521 /* define DBM class */
522 cDBM = rb_define_class("DBM", rb_cObject);
523 /* DBM includes Enumerate module */
524 rb_include_module(cDBM, rb_mEnumerable);
526 /* DBM has class method open(): arguments are received as C array */
527 rb_define_singleton_method(cDBM, "open", fdbm_s_open, -1);
529 /* DBM instance method close(): no args */
530 rb_define_method(cDBM, "close", fdbm_close, 0);
531 /* DBM instance method []: 1 argument */
532 rb_define_method(cDBM, "[]", fdbm_fetch, 1);
535 /* ID for a instance variable to store DBM data */
536 id_dbm = rb_intern("dbm");
540 The dbm extension wraps the dbm struct in the C environment using
550 obj = Data_Make_Struct(klass, struct dbmdata, 0, free_dbm, dbmp);
553 This code wraps the dbmdata structure into a Ruby object. We avoid
554 wrapping DBM* directly, because we want to cache size information.
556 To retrieve the dbmdata structure from a Ruby object, we define the
560 #define GetDBM(obj, dbmp) {\
561 Data_Get_Struct(obj, struct dbmdata, dbmp);\
562 if (dbmp->di_dbm == 0) closed_dbm();\
566 This sort of complicated macro does the retrieving and close checking for
569 There are three kinds of way to receive method arguments. First,
570 methods with a fixed number of arguments receive arguments like this:
574 fdbm_delete(VALUE obj, VALUE keystr)
580 The first argument of the C function is the self, the rest are the
581 arguments to the method.
583 Second, methods with an arbitrary number of arguments receive
588 fdbm_s_open(int argc, VALUE *argv, VALUE klass)
591 if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
592 mode = 0666; /* default value */
598 The first argument is the number of method arguments, the second
599 argument is the C array of the method arguments, and the third
600 argument is the receiver of the method.
602 You can use the function rb_scan_args() to check and retrieve the
603 arguments. For example, "11" means that the method requires at least one
604 argument, and at most receives two arguments.
606 Methods with an arbitrary number of arguments can receive arguments
607 by Ruby's array, like this:
611 fdbm_indexes(VALUE obj, VALUE args)
617 The first argument is the receiver, the second one is the Ruby array
618 which contains the arguments to the method.
622 GC should know about global variables which refer to Ruby's objects, but
623 are not exported to the Ruby world. You need to protect them by
625 void rb_global_variable(VALUE *var)
627 (4) prepare extconf.rb
629 If the file named extconf.rb exists, it will be executed to generate
632 extconf.rb is the file for checking compilation conditions etc. You
637 at the top of the file. You can use the functions below to check
640 have_library(lib, func): check whether library containing function exists.
641 have_func(func, header): check whether function exists
642 have_header(header): check whether header file exists
643 create_makefile(target): generate Makefile
645 The value of the variables below will affect the Makefile.
647 $CFLAGS: included in CFLAGS make variable (such as -O)
648 $CPPFLAGS: included in CPPFLAGS make variable (such as -I, -D)
649 $LDFLAGS: included in LDFLAGS make variable (such as -L)
650 $objs: list of object file names
652 Normally, the object files list is automatically generated by searching
653 source files, but you must define them explicitly if any sources will
654 be generated while building.
656 If a compilation condition is not fulfilled, you should not call
657 ``create_makefile''. The Makefile will not be generated, compilation will
660 (5) prepare depend (optional)
662 If the file named depend exists, Makefile will include that file to
663 check dependencies. You can make this file by invoking
665 % gcc -MM *.c > depend
667 It's harmless. Prepare it.
669 (6) generate Makefile
671 Try generating the Makefile by:
675 If the library should be installed under vendor_ruby directory
676 instead of site_ruby directory, use --vendor option as follows.
678 ruby extconf.rb --vendor
680 You don't need this step if you put the extension library under the ext
681 directory of the ruby source tree. In that case, compilation of the
682 interpreter will do this step for you.
690 to compile your extension. You don't need this step either if you have
691 put the extension library under the ext directory of the ruby source tree.
695 You may need to rb_debug the extension. Extensions can be linked
696 statically by adding the directory name in the ext/Setup file so that
697 you can inspect the extension with the debugger.
699 (9) done, now you have the extension library
701 You can do anything you want with your library. The author of Ruby
702 will not claim any restrictions on your code depending on the Ruby API.
703 Feel free to use, modify, distribute or sell your program.
705 Appendix A. Ruby source files overview
724 ruby interpreter implementation
757 Appendix B. Ruby extension API reference
763 The type for the Ruby object. Actual structures are defined in ruby.h,
764 such as struct RString, etc. To refer the values in structures, use
765 casting macros like RSTRING(obj).
767 ** Variables and constants
775 const: true object(default true value)
781 ** C pointer wrapping
783 Data_Wrap_Struct(VALUE klass, void (*mark)(), void (*free)(), void *sval)
785 Wrap a C pointer into a Ruby object. If object has references to other
786 Ruby objects, they should be marked by using the mark function during
787 the GC process. Otherwise, mark should be 0. When this object is no
788 longer referred by anywhere, the pointer will be discarded by free
791 Data_Make_Struct(klass, type, mark, free, sval)
793 This macro allocates memory using malloc(), assigns it to the variable
794 sval, and returns the DATA encapsulating the pointer to memory region.
796 Data_Get_Struct(data, type, sval)
798 This macro retrieves the pointer value from DATA, and assigns it to
801 ** Checking data types
806 void Check_Type(VALUE value, int type)
807 void Check_SafeStr(VALUE value)
809 ** Data type conversion
818 StringValuePtr(value)
819 StringValueCStr(value)
822 ** defining class/module
824 VALUE rb_define_class(const char *name, VALUE super)
826 Defines a new Ruby class as a subclass of super.
828 VALUE rb_define_class_under(VALUE module, const char *name, VALUE super)
830 Creates a new Ruby class as a subclass of super, under the module's
833 VALUE rb_define_module(const char *name)
835 Defines a new Ruby module.
837 VALUE rb_define_module_under(VALUE module, const char *name)
839 Defines a new Ruby module under the module's namespace.
841 void rb_include_module(VALUE klass, VALUE module)
843 Includes module into class. If class already includes it, just
846 void rb_extend_object(VALUE object, VALUE module)
848 Extend the object with the module's attributes.
850 ** Defining Global Variables
852 void rb_define_variable(const char *name, VALUE *var)
854 Defines a global variable which is shared between C and Ruby. If name
855 contains a character which is not allowed to be part of the symbol,
856 it can't be seen from Ruby programs.
858 void rb_define_readonly_variable(const char *name, VALUE *var)
860 Defines a read-only global variable. Works just like
861 rb_define_variable(), except the defined variable is read-only.
863 void rb_define_virtual_variable(const char *name,
864 VALUE (*getter)(), VALUE (*setter)())
866 Defines a virtual variable, whose behavior is defined by a pair of C
867 functions. The getter function is called when the variable is
868 referenced. The setter function is called when the variable is set to a
869 value. The prototype for getter/setter functions are:
872 void setter(VALUE val, ID id)
874 The getter function must return the value for the access.
876 void rb_define_hooked_variable(const char *name, VALUE *var,
877 VALUE (*getter)(), VALUE (*setter)())
879 Defines hooked variable. It's a virtual variable with a C variable.
880 The getter is called as
882 VALUE getter(ID id, VALUE *var)
884 returning a new value. The setter is called as
886 void setter(VALUE val, ID id, VALUE *var)
888 GC requires C global variables which hold Ruby values to be marked.
890 void rb_global_variable(VALUE *var)
892 Tells GC to protect these variables.
894 ** Constant Definition
896 void rb_define_const(VALUE klass, const char *name, VALUE val)
898 Defines a new constant under the class/module.
900 void rb_define_global_const(const char *name, VALUE val)
902 Defines a global constant. This is just the same as
904 rb_define_const(cKernal, name, val)
908 rb_define_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
910 Defines a method for the class. func is the function pointer. argc
911 is the number of arguments. if argc is -1, the function will receive
912 3 arguments: argc, argv, and self. if argc is -2, the function will
913 receive 2 arguments, self and args, where args is a Ruby array of
914 the method arguments.
916 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
918 Defines a private method for the class. Arguments are same as
921 rb_define_singleton_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
923 Defines a singleton method. Arguments are same as rb_define_method().
925 rb_scan_args(int argc, VALUE *argv, const char *fmt, ...)
927 Retrieve argument from argc, argv. The fmt is the format string for
928 the arguments, such as "12" for 1 non-optional argument, 2 optional
929 arguments. If `*' appears at the end of fmt, it means the rest of
930 the arguments are assigned to the corresponding variable, packed in
933 ** Invoking Ruby method
935 VALUE rb_funcall(VALUE recv, ID mid, int narg, ...)
937 Invokes a method. To retrieve mid from a method name, use rb_intern().
939 VALUE rb_funcall2(VALUE recv, ID mid, int argc, VALUE *argv)
941 Invokes a method, passing arguments by an array of values.
943 VALUE rb_eval_string(const char *str)
945 Compiles and executes the string as a Ruby program.
947 ID rb_intern(const char *name)
949 Returns ID corresponding to the name.
951 char *rb_id2name(ID id)
953 Returns the name corresponding ID.
955 char *rb_class2name(VALUE klass)
957 Returns the name of the class.
959 int rb_respond_to(VALUE object, ID id)
961 Returns true if the object responds to the message specified by id.
963 ** Instance Variables
965 VALUE rb_iv_get(VALUE obj, const char *name)
967 Retrieve the value of the instance variable. If the name is not
968 prefixed by `@', that variable shall be inaccessible from Ruby.
970 VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
972 Sets the value of the instance variable.
976 VALUE rb_iterate(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2)
978 Calls the function func1, supplying func2 as the block. func1 will be
979 called with the argument arg1. func2 receives the value from yield as
980 the first argument, arg2 as the second argument.
982 VALUE rb_yield(VALUE val)
984 Evaluates the block with value val.
986 VALUE rb_rescue(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2)
988 Calls the function func1, with arg1 as the argument. If an exception
989 occurs during func1, it calls func2 with arg2 as the argument. The
990 return value of rb_rescue() is the return value from func1 if no
991 exception occurs, from func2 otherwise.
993 VALUE rb_ensure(VALUE (*func1)(), void *arg1, void (*func2)(), void *arg2)
995 Calls the function func1 with arg1 as the argument, then calls func2
996 with arg2 if execution terminated. The return value from
997 rb_ensure() is that of func1.
999 ** Exceptions and Errors
1001 void rb_warn(const char *fmt, ...)
1003 Prints a warning message according to a printf-like format.
1005 void rb_warning(const char *fmt, ...)
1007 Prints a warning message according to a printf-like format, if
1010 void rb_raise(rb_eRuntimeError, const char *fmt, ...)
1012 Raises RuntimeError. The fmt is a format string just like printf().
1014 void rb_raise(VALUE exception, const char *fmt, ...)
1016 Raises a class exception. The fmt is a format string just like printf().
1018 void rb_fatal(const char *fmt, ...)
1020 Raises a fatal error, terminates the interpreter. No exception handling
1021 will be done for fatal errors, but ensure blocks will be executed.
1023 void rb_bug(const char *fmt, ...)
1025 Terminates the interpreter immediately. This function should be
1026 called under the situation caused by the bug in the interpreter. No
1027 exception handling nor ensure execution will be done.
1029 ** Initialize and Start the Interpreter
1031 The embedding API functions are below (not needed for extension libraries):
1035 Initializes the interpreter.
1037 void ruby_options(int argc, char **argv)
1039 Process command line arguments for the interpreter.
1043 Starts execution of the interpreter.
1045 void ruby_script(char *name)
1047 Specifies the name of the script ($0).
1049 ** Hooks for the Interpreter Events
1051 void rb_add_event_hook(rb_event_hook_func_t func, rb_event_t events)
1053 Adds a hook function for the specified interpreter events.
1054 events should be Or'ed value of:
1066 The definition of rb_event_hook_func_t is below:
1068 typedef void (*rb_event_hook_func_t)(rb_event_t event, NODE *node,
1069 VALUE self, ID id, VALUE klass)
1071 int rb_remove_event_hook(rb_event_hook_func_t func)
1073 Removes the specified hook function.
1075 Appendix C. Functions Available in extconf.rb
1077 These functions are available in extconf.rb:
1079 have_macro(macro, headers)
1081 Checks whether macro is defined with header. Returns true if the macro
1084 have_library(lib, func)
1086 Checks whether the library exists, containing the specified function.
1087 Returns true if the library exists.
1089 find_library(lib, func, path...)
1091 Checks whether a library which contains the specified function exists in
1092 path. Returns true if the library exists.
1094 have_func(func, header)
1096 Checks whether func exists with header. Returns true if the function
1097 exists. To check functions in an additional library, you need to
1098 check that library first using have_library().
1100 have_var(var, header)
1102 Checks whether var exists with header. Returns true if the variable
1103 exists. To check variables in an additional library, you need to
1104 check that library first using have_library().
1108 Checks whether header exists. Returns true if the header file exists.
1110 find_header(header, path...)
1112 Checks whether header exists in path. Returns true if the header file
1115 have_struct_member(type, member, header)
1117 Checks whether type has member with header. Returns true if the type
1118 is defined and has the member.
1120 have_type(type, header, opt)
1122 Checks whether type is defined with header. Returns true if the type
1125 check_sizeof(type, header)
1127 Checks the size of type in char with header. Returns the size if the
1128 type is defined, otherwise nil.
1130 create_makefile(target)
1132 Generates the Makefile for the extension library. If you don't invoke
1133 this method, the compilation will not be done.
1135 find_executable(bin, path)
1137 Finds command in path, which is File::PATH_SEPARATOR-separated list of
1138 directories. If path is nil or omitted, environment variable PATH
1139 will be used. Returns the path name of the command if it is found,
1142 with_config(withval[, default=nil])
1144 Parses the command line options and returns the value specified by
1147 enable_config(config, *defaults)
1148 disable_config(config, *defaults)
1150 Parses the command line options for boolean. Returns true if
1151 --enable-<config> is given, or false if --disable-<config> is given.
1152 Otherwise, yields defaults to the given block and returns the result
1153 if it is called with a block, or returns defaults.
1155 dir_config(target[, default_dir])
1156 dir_config(target[, default_include, default_lib])
1158 Parses the command line options and adds the directories specified by
1159 --with-<target>-dir, --with-<target>-include, and/or --with-<target>-lib
1160 to $CFLAGS and/or $LDFLAGS. --with-<target>-dir=/path is equivalent to
1161 --with-<target>-include=/path/include --with-<target>-lib=/path/lib.
1162 Returns an array of the added directories ([include_dir, lib_dir]).
1166 Obtains the information for pkg by pkg-config command. The actual
1167 command name can be overridden by --with-pkg-config command line