1998-01-16 07:13:05 -05:00
|
|
|
.\" README.EXT - -*- Text -*- created at: Mon Aug 7 16:45:54 JST 1995
|
|
|
|
|
1999-08-24 04:21:56 -04:00
|
|
|
This document explains how to make extension libraries for Ruby.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
1. Basic knowledge
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
In C, variables have types and data do not have types. In contrast,
|
2002-10-23 04:20:35 -04:00
|
|
|
Ruby variables do not have a static type, and data themselves have
|
|
|
|
types, so data will need to be converted between the languages.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2006-09-13 05:49:58 -04:00
|
|
|
Data in Ruby are represented by the C type `VALUE'. Each VALUE data
|
|
|
|
has its data-type.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
To retrieve C data from a VALUE, you need to:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
(1) Identify the VALUE's data type
|
|
|
|
(2) Convert the VALUE into C data
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Converting to the wrong data type may cause serious problems.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
1.1 Data-types
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
The Ruby interpreter has the following data types:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
T_NIL nil
|
1999-08-24 04:21:56 -04:00
|
|
|
T_OBJECT ordinary object
|
1998-01-16 07:13:05 -05:00
|
|
|
T_CLASS class
|
|
|
|
T_MODULE module
|
|
|
|
T_FLOAT floating point number
|
|
|
|
T_STRING string
|
|
|
|
T_REGEXP regular expression
|
|
|
|
T_ARRAY array
|
1999-08-24 04:21:56 -04:00
|
|
|
T_HASH associative array
|
1999-01-19 23:59:39 -05:00
|
|
|
T_STRUCT (Ruby) structure
|
1998-01-16 07:13:05 -05:00
|
|
|
T_BIGNUM multi precision integer
|
2008-09-01 01:08:44 -04:00
|
|
|
T_FIXNUM Fixnum(31bit or 63bit integer)
|
|
|
|
T_COMPLEX complex number
|
|
|
|
T_RATIONAL rational number
|
2003-03-20 01:27:22 -05:00
|
|
|
T_FILE IO
|
1998-01-16 07:13:05 -05:00
|
|
|
T_TRUE true
|
|
|
|
T_FALSE false
|
|
|
|
T_DATA data
|
2000-10-05 05:57:04 -04:00
|
|
|
T_SYMBOL symbol
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
In addition, there are several other types used internally:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
T_ICLASS
|
|
|
|
T_MATCH
|
2000-10-05 05:57:04 -04:00
|
|
|
T_UNDEF
|
1998-01-16 07:13:05 -05:00
|
|
|
T_NODE
|
2008-09-01 01:08:44 -04:00
|
|
|
T_ZOMBIE
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
Most of the types are represented by C structures.
|
|
|
|
|
|
|
|
1.2 Check Data Type of the VALUE
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
The macro TYPE() defined in ruby.h shows the data type of the VALUE.
|
1998-01-16 07:13:05 -05:00
|
|
|
TYPE() returns the constant number T_XXXX described above. To handle
|
2002-10-23 04:20:35 -04:00
|
|
|
data types, your code will look something like this:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
switch (TYPE(obj)) {
|
|
|
|
case T_FIXNUM:
|
|
|
|
/* process Fixnum */
|
|
|
|
break;
|
|
|
|
case T_STRING:
|
|
|
|
/* process String */
|
|
|
|
break;
|
|
|
|
case T_ARRAY:
|
|
|
|
/* process Array */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* raise exception */
|
2000-03-05 23:15:42 -05:00
|
|
|
rb_raise(rb_eTypeError, "not valid value");
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
There is the data-type check function
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
void Check_Type(VALUE value, int type)
|
|
|
|
|
2007-12-23 16:01:25 -05:00
|
|
|
which raises an exception if the VALUE does not have the type
|
|
|
|
specified.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
There are also faster check macros for fixnums and nil.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
FIXNUM_P(obj)
|
|
|
|
NIL_P(obj)
|
|
|
|
|
|
|
|
1.3 Convert VALUE into C data
|
|
|
|
|
2009-09-15 11:42:41 -04:00
|
|
|
The data for type T_NIL, T_FALSE, T_TRUE are nil, false, true
|
1999-01-19 23:59:39 -05:00
|
|
|
respectively. They are singletons for the data type.
|
2009-09-15 11:42:41 -04:00
|
|
|
The equivalent C constants are: Qnil, Qfalse, Qtrue.
|
|
|
|
Note that Qfalse is false in C also (i.e. 0), but not Qnil.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2009-12-30 06:45:32 -05:00
|
|
|
The T_FIXNUM data is a 31bit or 63bit length fixed integer.
|
|
|
|
This size is depend on the size of long: if long is 32bit then
|
|
|
|
T_FIXNUM is 31bit, if long is 64bit then T_FIXNUM is 63bit.
|
|
|
|
T_FIXNUM can be converted to a C integer by using the
|
2008-07-22 05:09:41 -04:00
|
|
|
FIX2INT() macro or FIX2LONG(). Though you have to check that the
|
|
|
|
data is really FIXNUM before using them, they are faster. FIX2LONG()
|
|
|
|
never raises exceptions, but FIX2INT() raises RangeError if the
|
|
|
|
result is bigger or smaller than the size of int.
|
|
|
|
There are also NUM2INT() and NUM2LONG() which converts any Ruby
|
|
|
|
numbers into C integers. These macros includes a type check,
|
2007-12-23 16:01:25 -05:00
|
|
|
so an exception will be raised if the conversion failed. NUM2DBL()
|
|
|
|
can be used to retrieve the double float value in the same way.
|
2003-03-26 02:01:14 -05:00
|
|
|
|
2009-01-31 08:30:17 -05:00
|
|
|
You can use the macros
|
2006-09-13 10:45:21 -04:00
|
|
|
StringValue() and StringValuePtr() to get a char* from a VALUE.
|
|
|
|
StringValue(var) replaces var's value with the result of "var.to_str()".
|
|
|
|
StringValuePtr(var) does same replacement and returns char*
|
2007-12-23 16:01:25 -05:00
|
|
|
representation of var. These macros will skip the replacement if var
|
|
|
|
is a String. Notice that the macros take only the lvalue as their
|
2006-09-13 10:45:21 -04:00
|
|
|
argument, to change the value of var in place.
|
2003-03-26 02:01:14 -05:00
|
|
|
|
2008-06-13 01:56:51 -04:00
|
|
|
You can also use the macro named StringValueCStr(). This is just
|
|
|
|
like StringValuePtr(), but always add nul character at the end of
|
|
|
|
the result. If the result contains nul character, this macro causes
|
|
|
|
the ArgumentError exception.
|
2009-09-15 11:42:41 -04:00
|
|
|
StringValuePtr() doesn't guarantee the existence of a nul at the end
|
|
|
|
of the result, and the result may contain nul.
|
2008-06-13 01:56:51 -04:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Other data types have corresponding C structures, e.g. struct RArray
|
2007-12-23 16:01:25 -05:00
|
|
|
for T_ARRAY etc. The VALUE of the type which has the corresponding
|
|
|
|
structure can be cast to retrieve the pointer to the struct. The
|
|
|
|
casting macro will be of the form RXXXX for each data type; for
|
|
|
|
instance, RARRAY(obj). See "ruby.h".
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2006-09-16 06:51:35 -04:00
|
|
|
There are some accessing macros for structure members, for example
|
2009-06-03 20:53:19 -04:00
|
|
|
`RSTRING_LEN(str)' to get the size of the Ruby String object. The
|
|
|
|
allocated region can be accessed by `RSTRING_PTR(str)'. For arrays,
|
|
|
|
use `RARRAY_LEN(ary)' and `RARRAY_PTR(ary)' respectively.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Notice: Do not change the value of the structure directly, unless you
|
2007-12-23 16:01:25 -05:00
|
|
|
are responsible for the result. This ends up being the cause of
|
|
|
|
interesting bugs.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
1.4 Convert C data into VALUE
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
To convert C data to Ruby values:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
* FIXNUM
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
left shift 1 bit, and turn on LSB.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
* Other pointer values
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
cast to VALUE.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
You can determine whether a VALUE is pointer or not by checking its LSB.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Notice Ruby does not allow arbitrary pointer values to be a VALUE. They
|
|
|
|
should be pointers to the structures which Ruby knows about. The known
|
1999-08-13 01:45:20 -04:00
|
|
|
structures are defined in <ruby.h>.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
To convert C numbers to Ruby values, use these macros.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-24 04:21:56 -04:00
|
|
|
INT2FIX() for integers within 31bits.
|
1999-08-13 01:45:20 -04:00
|
|
|
INT2NUM() for arbitrary sized integer.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
INT2NUM() converts an integer into a Bignum if it is out of the FIXNUM
|
|
|
|
range, but is a bit slower.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
1.5 Manipulating Ruby data
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2007-12-23 16:01:25 -05:00
|
|
|
As I already mentioned, it is not recommended to modify an object's
|
|
|
|
internal structure. To manipulate objects, use the functions supplied
|
|
|
|
by the Ruby interpreter. Some (not all) of the useful functions are
|
|
|
|
listed below:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-24 04:21:56 -04:00
|
|
|
String functions
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
rb_str_new(const char *ptr, long len)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Creates a new Ruby string.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
rb_str_new2(const char *ptr)
|
2008-09-01 01:08:44 -04:00
|
|
|
rb_str_new_cstr(const char *ptr)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Creates a new Ruby string from a C string. This is equivalent to
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_new(ptr, strlen(ptr)).
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
rb_tainted_str_new(const char *ptr, long len)
|
2000-07-10 00:49:24 -04:00
|
|
|
|
|
|
|
Creates a new tainted Ruby string. Strings from external data
|
2002-10-23 04:20:35 -04:00
|
|
|
sources should be tainted.
|
2000-07-10 00:49:24 -04:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
rb_tainted_str_new2(const char *ptr)
|
2008-09-01 01:08:44 -04:00
|
|
|
rb_tainted_str_new_cstr(const char *ptr)
|
2000-07-10 00:49:24 -04:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Creates a new tainted Ruby string from a C string.
|
2000-07-10 00:49:24 -04:00
|
|
|
|
2008-07-21 00:55:40 -04:00
|
|
|
rb_sprintf(const char *format, ...)
|
2008-07-21 13:50:52 -04:00
|
|
|
rb_vsprintf(const char *format, va_list ap)
|
2008-07-21 00:55:40 -04:00
|
|
|
|
|
|
|
Creates a new Ruby string with printf(3) format.
|
|
|
|
|
2008-07-22 03:48:00 -04:00
|
|
|
rb_str_cat(VALUE str, const char *ptr, long len)
|
|
|
|
|
|
|
|
Appends len bytes of data from ptr to the Ruby string.
|
|
|
|
|
2008-07-21 13:50:52 -04:00
|
|
|
rb_str_cat2(VALUE str, const char* ptr)
|
|
|
|
|
|
|
|
Appends C string ptr to Ruby string str. This function is
|
|
|
|
equivalent to rb_str_cat(str, ptr, strlen(ptr)).
|
|
|
|
|
2008-07-22 03:48:00 -04:00
|
|
|
rb_str_catf(VALUE str, const char* format, ...)
|
|
|
|
rb_str_vcatf(VALUE str, const char* format, va_list ap)
|
|
|
|
|
|
|
|
Appends C string format and successive arguments to Ruby string
|
|
|
|
str according to a printf-like format. These functions are
|
|
|
|
equivalent to rb_str_cat2(str, rb_sprintf(format, ...)) and
|
|
|
|
rb_str_cat2(str, rb_vsprintf(format, ap)), respectively.
|
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
|
|
|
|
|
|
|
|
Creates a new Ruby string with the specified encoding.
|
|
|
|
|
|
|
|
rb_usascii_str_new(const char *ptr, long len)
|
|
|
|
rb_usascii_str_new_cstr(const char *ptr)
|
|
|
|
|
|
|
|
Creates a new Ruby string with encoding US-ASCII.
|
|
|
|
|
2010-08-05 07:14:05 -04:00
|
|
|
rb_str_resize(VALUE str, long len)
|
|
|
|
|
|
|
|
Resizes Ruby string to len bytes. If str is not modifiable, this
|
|
|
|
function raises an exception. The length of str must be set in
|
|
|
|
advance. If len is less than the old length the content beyond
|
|
|
|
len bytes is discarded, else if len is greater than the old length
|
|
|
|
the content beyond the old length bytes will not be preserved but
|
|
|
|
will be garbage. Note that RSTRING_PTR(str) may change by calling
|
|
|
|
this function.
|
|
|
|
|
|
|
|
rb_str_set_len(VALUE str, long len)
|
|
|
|
|
|
|
|
Sets the length of Ruby string. If str is not modifiable, this
|
|
|
|
function raises an exception. This function preserves the content
|
|
|
|
upto len bytes, regardless RSTRING_LEN(str). len must not exceed
|
|
|
|
the capacity of str.
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
Array functions
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_new()
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Creates an array with no elements.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
rb_ary_new2(long len)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Creates an array with no elements, allocating internal buffer
|
1998-01-16 07:13:05 -05:00
|
|
|
for len elements.
|
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
rb_ary_new3(long n, ...)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Creates an n-element array from the arguments.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
rb_ary_new4(long n, VALUE *elts)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Creates an n-element array from a C array.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
rb_ary_to_ary(VALUE obj)
|
|
|
|
|
|
|
|
Converts the object into an array.
|
|
|
|
Equivalent to Object#to_ary.
|
|
|
|
|
|
|
|
There are many functions to operate an array.
|
|
|
|
They may dump core if other types are given.
|
|
|
|
|
|
|
|
rb_ary_aref(argc, VALUE *argv, VALUE ary)
|
|
|
|
|
|
|
|
Equivaelent to Array#[].
|
|
|
|
|
|
|
|
rb_ary_entry(VALUE ary, long offset)
|
|
|
|
|
|
|
|
ary[offset]
|
|
|
|
|
|
|
|
rb_ary_subseq(VALUE ary, long beg, long len)
|
|
|
|
|
|
|
|
ary[beg, len]
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(VALUE ary, VALUE val)
|
|
|
|
rb_ary_pop(VALUE ary)
|
|
|
|
rb_ary_shift(VALUE ary)
|
|
|
|
rb_ary_unshift(VALUE ary, VALUE val)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
2. Extending Ruby with C
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2007-12-23 16:01:25 -05:00
|
|
|
2.1 Adding new features to Ruby
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
You can add new features (classes, methods, etc.) to the Ruby
|
2002-10-23 04:20:35 -04:00
|
|
|
interpreter. Ruby provides APIs for defining the following things:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
* Classes, Modules
|
|
|
|
* Methods, Singleton Methods
|
|
|
|
* Constants
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
2.1.1 Class/module definition
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
To define a class or module, use the functions below:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
VALUE rb_define_class(const char *name, VALUE super)
|
|
|
|
VALUE rb_define_module(const char *name)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-24 04:21:56 -04:00
|
|
|
These functions return the newly created class or module. You may
|
2002-10-23 04:20:35 -04:00
|
|
|
want to save this reference into a variable to use later.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
To define nested classes or modules, use the functions below:
|
2000-03-05 23:15:42 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
|
|
|
|
VALUE rb_define_module_under(VALUE outer, const char *name)
|
2000-03-05 23:15:42 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
2.1.2 Method/singleton method definition
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
To define methods or singleton methods, use these functions:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_method(VALUE klass, const char *name,
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE (*func)(), int argc)
|
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_singleton_method(VALUE object, const char *name,
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE (*func)(), int argc)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
The `argc' represents the number of the arguments to the C function,
|
2006-09-13 05:49:58 -04:00
|
|
|
which must be less than 17. But I doubt you'll need that many.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
If `argc' is negative, it specifies the calling sequence, not number of
|
1999-01-19 23:59:39 -05:00
|
|
|
the arguments.
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
If argc is -1, the function will be called as:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE func(int argc, VALUE *argv, VALUE obj)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
where argc is the actual number of arguments, argv is the C array of
|
|
|
|
the arguments, and obj is the receiver.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
If argc is -2, the arguments are passed in a Ruby array. The function
|
1999-01-19 23:59:39 -05:00
|
|
|
will be called like:
|
|
|
|
|
|
|
|
VALUE func(VALUE obj, VALUE args)
|
|
|
|
|
|
|
|
where obj is the receiver, and args is the Ruby array containing
|
|
|
|
actual arguments.
|
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
There are some more functions to define methods. One takes an ID
|
|
|
|
as the name of method to be defined. See 2.2.2 for IDs.
|
|
|
|
|
|
|
|
void rb_define_method_id(VALUE klass, ID name,
|
|
|
|
VALUE (*func)(ANYARGS), int argc)
|
|
|
|
|
|
|
|
There are two functions to define private/protected methods:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_private_method(VALUE klass, const char *name,
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE (*func)(), int argc)
|
2008-09-01 01:08:44 -04:00
|
|
|
void rb_define_protected_method(VALUE klass, const char *name,
|
|
|
|
VALUE (*func)(), int argc)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2009-09-15 11:42:41 -04:00
|
|
|
At last, rb_define_module_function defines a module functions,
|
2008-09-01 01:08:44 -04:00
|
|
|
which are private AND singleton methods of the module.
|
|
|
|
For example, sqrt is the module function defined in Math module.
|
|
|
|
It can be called in the following way:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
Math.sqrt(4)
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
or
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
include Math
|
|
|
|
sqrt(4)
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
To define module functions, use:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_module_function(VALUE module, const char *name,
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE (*func)(), int argc)
|
|
|
|
|
2009-09-15 11:42:41 -04:00
|
|
|
In addition, function-like methods, which are private methods defined
|
2002-10-23 04:20:35 -04:00
|
|
|
in the Kernel module, can be defined using:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_global_function(const char *name, VALUE (*func)(), int argc)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2006-09-13 05:49:58 -04:00
|
|
|
To define an alias for the method,
|
2000-07-10 00:49:24 -04:00
|
|
|
|
|
|
|
void rb_define_alias(VALUE module, const char* new, const char* old);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2009-09-15 11:42:41 -04:00
|
|
|
To define a reader/writer for an attribute,
|
2008-09-01 01:08:44 -04:00
|
|
|
|
|
|
|
void rb_define_attr(VALUE klass, const char *name, int read, int write)
|
|
|
|
|
2005-09-20 19:20:58 -04:00
|
|
|
To define and undefine the `allocate' class method,
|
|
|
|
|
|
|
|
void rb_define_alloc_func(VALUE klass, VALUE (*func)(VALUE klass));
|
|
|
|
void rb_undef_alloc_func(VALUE klass);
|
|
|
|
|
2009-09-15 11:42:41 -04:00
|
|
|
func has to take the klass as the argument and return a newly
|
|
|
|
allocated instance. This instance should be as empty as possible,
|
2005-09-20 19:20:58 -04:00
|
|
|
without any expensive (including external) resources.
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
2.1.3 Constant definition
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
We have 2 functions to define constants:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_const(VALUE klass, const char *name, VALUE val)
|
|
|
|
void rb_define_global_const(const char *name, VALUE val)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
The former is to define a constant under specified class/module. The
|
|
|
|
latter is to define a global constant.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
2.2 Use Ruby features from C
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
There are several ways to invoke Ruby's features from C code.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
2.2.1 Evaluate Ruby Programs in a String
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
The easiest way to use Ruby's functionality from a C program is to
|
2006-09-13 05:49:58 -04:00
|
|
|
evaluate the string as Ruby program. This function will do the job:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
VALUE rb_eval_string(const char *str)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Evaluation is done under the current context, thus current local variables
|
1999-01-19 23:59:39 -05:00
|
|
|
of the innermost method (which is defined by Ruby) can be accessed.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
Note that the evaluation can raise an exception. There is a safer
|
|
|
|
function:
|
|
|
|
|
|
|
|
VALUE rb_eval_string_protect(const char *str, int *state)
|
|
|
|
|
2009-09-15 11:42:41 -04:00
|
|
|
It returns nil when an error occur. Moreover, *state is zero if str was
|
2008-09-01 01:08:44 -04:00
|
|
|
successfully evaluated, or nonzero otherwise.
|
|
|
|
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
2.2.2 ID or Symbol
|
|
|
|
|
2007-12-23 16:01:25 -05:00
|
|
|
You can invoke methods directly, without parsing the string. First I
|
|
|
|
need to explain about ID. ID is the integer number to represent
|
|
|
|
Ruby's identifiers such as variable names. The Ruby data type
|
|
|
|
corresponding to ID is Symbol. It can be accessed from Ruby in the
|
|
|
|
form:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
:Identifier
|
2008-09-01 01:08:44 -04:00
|
|
|
or
|
|
|
|
:"any kind of string"
|
1998-01-16 07:13:05 -05:00
|
|
|
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-09 17:20:17 -04:00
|
|
|
You can get the ID value from a string within C code by using
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
rb_intern(const char *name)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-09 17:20:17 -04:00
|
|
|
You can retrieve ID from Ruby object (Symbol or String) given as an
|
|
|
|
argument by using
|
|
|
|
|
|
|
|
rb_to_id(VALUE symbol)
|
|
|
|
|
|
|
|
You can convert C ID to Ruby Symbol by using
|
|
|
|
|
|
|
|
VALUE ID2SYM(ID id)
|
|
|
|
|
|
|
|
and to convert Ruby Symbol object to ID, use
|
|
|
|
|
|
|
|
ID SYM2ID(VALUE symbol)
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
2.2.3 Invoke Ruby method from C
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
To invoke methods directly, you can use the function below
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
VALUE rb_funcall(VALUE recv, ID mid, int argc, ...)
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
This function invokes a method on the recv, with the method name
|
|
|
|
specified by the symbol mid.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
2.2.4 Accessing the variables and constants
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
You can access class variables and instance variables using access
|
2007-12-23 16:01:25 -05:00
|
|
|
functions. Also, global variables can be shared between both
|
|
|
|
environments. There's no way to access Ruby's local variables.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
The functions to access/modify instance variables are below:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
VALUE rb_ivar_get(VALUE obj, ID id)
|
|
|
|
VALUE rb_ivar_set(VALUE obj, ID id, VALUE val)
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
id must be the symbol, which can be retrieved by rb_intern().
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
To access the constants of the class/module:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
VALUE rb_const_get(VALUE obj, ID id)
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
See 2.1.3 for defining new constant.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-24 04:21:56 -04:00
|
|
|
3. Information sharing between Ruby and C
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
3.1 Ruby constants that C can be accessed from C
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2009-09-15 11:42:41 -04:00
|
|
|
As stated in section 1.3,
|
|
|
|
the following Ruby constants can be referred from C.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Qtrue
|
|
|
|
Qfalse
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Boolean values. Qfalse is false in C also (i.e. 0).
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
Qnil
|
|
|
|
|
|
|
|
Ruby nil in C scope.
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
3.2 Global variables shared between C and Ruby
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Information can be shared between the two environments using shared global
|
1999-08-13 01:45:20 -04:00
|
|
|
variables. To define them, you can use functions listed below:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_variable(const char *name, VALUE *var)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
This function defines the variable which is shared by both environments.
|
|
|
|
The value of the global variable pointed to by `var' can be accessed
|
1999-08-13 01:45:20 -04:00
|
|
|
through Ruby's global variable named `name'.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
You can define read-only (from Ruby, of course) variables using the
|
1999-08-13 01:45:20 -04:00
|
|
|
function below.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_readonly_variable(const char *name, VALUE *var)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
You can defined hooked variables. The accessor functions (getter and
|
|
|
|
setter) are called on access to the hooked variables.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
void rb_define_hooked_variable(const char *name, VALUE *var,
|
2000-10-05 05:57:04 -04:00
|
|
|
VALUE (*getter)(), void (*setter)())
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
If you need to supply either setter or getter, just supply 0 for the
|
|
|
|
hook you don't need. If both hooks are 0, rb_define_hooked_variable()
|
|
|
|
works just like rb_define_variable().
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
The prototypes of the getter and setter functions are as follows:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
VALUE (*getter)(ID id, VALUE *var);
|
|
|
|
void (*setter)(VALUE val, ID id, VALUE *var);
|
|
|
|
|
|
|
|
|
|
|
|
Also you can define a Ruby global variable without a corresponding C
|
1999-08-13 01:45:20 -04:00
|
|
|
variable. The value of the variable will be set/get only by hooks.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
void rb_define_virtual_variable(const char *name,
|
|
|
|
VALUE (*getter)(), void (*setter)())
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
The prototypes of the getter and setter functions are as follows:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
VALUE (*getter)(ID id);
|
|
|
|
void (*setter)(VALUE val, ID id);
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2006-09-13 05:49:58 -04:00
|
|
|
3.3 Encapsulate C data into a Ruby object
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
To wrap and objectify a C pointer as a Ruby object (so called
|
1999-08-13 01:45:20 -04:00
|
|
|
DATA), use Data_Wrap_Struct().
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-03-05 23:15:42 -05:00
|
|
|
Data_Wrap_Struct(klass, mark, free, ptr)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-03-05 23:15:42 -05:00
|
|
|
Data_Wrap_Struct() returns a created DATA object. The klass argument
|
1999-08-13 01:45:20 -04:00
|
|
|
is the class for the DATA object. The mark argument is the function
|
|
|
|
to mark Ruby objects pointed by this data. The free argument is the
|
2003-04-02 01:11:28 -05:00
|
|
|
function to free the pointer allocation. If this is -1, the pointer
|
|
|
|
will be just freed. The functions mark and free will be called from
|
|
|
|
garbage collector.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2008-06-21 20:24:45 -04:00
|
|
|
These mark / free functions are invoked during GC execution. No
|
|
|
|
object allocations are allowed during it, so do not allocate ruby
|
|
|
|
objects inside them.
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
You can allocate and wrap the structure in one step.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-24 04:21:56 -04:00
|
|
|
Data_Make_Struct(klass, type, mark, free, sval)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
This macro returns an allocated Data object, wrapping the pointer to
|
|
|
|
the structure, which is also allocated. This macro works like:
|
|
|
|
|
1999-08-24 04:21:56 -04:00
|
|
|
(sval = ALLOC(type), Data_Wrap_Struct(klass, mark, free, sval))
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Arguments klass, mark, and free work like their counterparts in
|
|
|
|
Data_Wrap_Struct(). A pointer to the allocated structure will be
|
|
|
|
assigned to sval, which should be a pointer of the type specified.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
To retrieve the C pointer from the Data object, use the macro
|
|
|
|
Data_Get_Struct().
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
Data_Get_Struct(obj, type, sval)
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
A pointer to the structure will be assigned to the variable sval.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
See the example below for details.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
4. Example - Creating dbm extension
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
OK, here's the example of making an extension library. This is the
|
|
|
|
extension to access DBMs. The full source is included in the ext/
|
1999-08-13 01:45:20 -04:00
|
|
|
directory in the Ruby's source tree.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
(1) make the directory
|
|
|
|
|
|
|
|
% mkdir ext/dbm
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Make a directory for the extension library under ext directory.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-11-01 05:41:08 -05:00
|
|
|
(2) design the library
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
You need to design the library features, before making it.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-11-01 05:41:08 -05:00
|
|
|
(3) write C code.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
You need to write C code for your extension library. If your library
|
|
|
|
has only one source file, choosing ``LIBRARY.c'' as a file name is
|
2002-10-23 04:20:35 -04:00
|
|
|
preferred. On the other hand, in case your library has multiple source
|
1999-08-24 04:21:56 -04:00
|
|
|
files, avoid choosing ``LIBRARY.c'' for a file name. It may conflict
|
2002-10-23 04:20:35 -04:00
|
|
|
with an intermediate file ``LIBRARY.o'' on some platforms.
|
2011-01-20 02:13:58 -05:00
|
|
|
Note that some functions in mkmf library described below generate
|
|
|
|
a file ``conftest.c'' for checking with compilation. You shouldn't
|
|
|
|
choose ``conftest.c'' as a name of a source file.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Ruby will execute the initializing function named ``Init_LIBRARY'' in
|
1999-08-24 04:21:56 -04:00
|
|
|
the library. For example, ``Init_dbm()'' will be executed when loading
|
1999-08-13 01:45:20 -04:00
|
|
|
the library.
|
|
|
|
|
|
|
|
Here's the example of an initializing function.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
--
|
2007-12-23 16:01:25 -05:00
|
|
|
void
|
|
|
|
Init_dbm(void)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
/* define DBM class */
|
|
|
|
cDBM = rb_define_class("DBM", rb_cObject);
|
|
|
|
/* DBM includes Enumerate module */
|
|
|
|
rb_include_module(cDBM, rb_mEnumerable);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
/* DBM has class method open(): arguments are received as C array */
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_define_singleton_method(cDBM, "open", fdbm_s_open, -1);
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
/* DBM instance method close(): no args */
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_define_method(cDBM, "close", fdbm_close, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
/* DBM instance method []: 1 argument */
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_define_method(cDBM, "[]", fdbm_fetch, 1);
|
|
|
|
:
|
|
|
|
|
2003-03-20 01:27:22 -05:00
|
|
|
/* ID for a instance variable to store DBM data */
|
|
|
|
id_dbm = rb_intern("dbm");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
--
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
The dbm extension wraps the dbm struct in the C environment using
|
|
|
|
Data_Make_Struct.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
--
|
|
|
|
struct dbmdata {
|
|
|
|
int di_size;
|
|
|
|
DBM *di_dbm;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-03-05 23:15:42 -05:00
|
|
|
obj = Data_Make_Struct(klass, struct dbmdata, 0, free_dbm, dbmp);
|
1998-01-16 07:13:05 -05:00
|
|
|
--
|
|
|
|
|
2007-12-23 16:01:25 -05:00
|
|
|
This code wraps the dbmdata structure into a Ruby object. We avoid
|
|
|
|
wrapping DBM* directly, because we want to cache size information.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
To retrieve the dbmdata structure from a Ruby object, we define the
|
|
|
|
following macro:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
--
|
|
|
|
#define GetDBM(obj, dbmp) {\
|
|
|
|
Data_Get_Struct(obj, struct dbmdata, dbmp);\
|
|
|
|
if (dbmp->di_dbm == 0) closed_dbm();\
|
|
|
|
}
|
|
|
|
--
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
This sort of complicated macro does the retrieving and close checking for
|
1999-08-13 01:45:20 -04:00
|
|
|
the DBM.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
There are three kinds of way to receive method arguments. First,
|
|
|
|
methods with a fixed number of arguments receive arguments like this:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
--
|
|
|
|
static VALUE
|
2007-12-23 16:01:25 -05:00
|
|
|
fdbm_delete(VALUE obj, VALUE keystr)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
:
|
|
|
|
}
|
|
|
|
--
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
The first argument of the C function is the self, the rest are the
|
|
|
|
arguments to the method.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Second, methods with an arbitrary number of arguments receive
|
1999-08-13 01:45:20 -04:00
|
|
|
arguments like this:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
--
|
|
|
|
static VALUE
|
2007-12-23 16:01:25 -05:00
|
|
|
fdbm_s_open(int argc, VALUE *argv, VALUE klass)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
:
|
|
|
|
if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
|
|
|
|
mode = 0666; /* default value */
|
|
|
|
}
|
|
|
|
:
|
|
|
|
}
|
|
|
|
--
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
The first argument is the number of method arguments, the second
|
|
|
|
argument is the C array of the method arguments, and the third
|
1999-08-13 01:45:20 -04:00
|
|
|
argument is the receiver of the method.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
You can use the function rb_scan_args() to check and retrieve the
|
2009-02-16 00:54:17 -05:00
|
|
|
arguments. The third argument is a string that specifies how to
|
2009-02-18 13:34:38 -05:00
|
|
|
capture method arguments and assign them to the following VALUE
|
|
|
|
references.
|
2009-02-16 00:54:17 -05:00
|
|
|
|
|
|
|
|
2009-02-18 13:34:38 -05:00
|
|
|
The following is an example of a method that takes arguments by Ruby's
|
|
|
|
array:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
--
|
|
|
|
static VALUE
|
2009-02-16 00:54:17 -05:00
|
|
|
thread_initialize(VALUE thread, VALUE args)
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
:
|
|
|
|
}
|
|
|
|
--
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
The first argument is the receiver, the second one is the Ruby array
|
|
|
|
which contains the arguments to the method.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
** Notice
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
GC should know about global variables which refer to Ruby's objects, but
|
|
|
|
are not exported to the Ruby world. You need to protect them by
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
void rb_global_variable(VALUE *var)
|
|
|
|
|
2004-11-01 05:41:08 -05:00
|
|
|
(4) prepare extconf.rb
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
If the file named extconf.rb exists, it will be executed to generate
|
2004-11-09 22:31:55 -05:00
|
|
|
Makefile.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2006-09-13 05:49:58 -04:00
|
|
|
extconf.rb is the file for checking compilation conditions etc. You
|
1999-08-13 01:45:20 -04:00
|
|
|
need to put
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
require 'mkmf'
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
at the top of the file. You can use the functions below to check
|
|
|
|
various conditions.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
have_library(lib, func): check whether library containing function exists.
|
2000-07-06 23:20:53 -04:00
|
|
|
have_func(func, header): check whether function exists
|
1999-08-13 01:45:20 -04:00
|
|
|
have_header(header): check whether header file exists
|
|
|
|
create_makefile(target): generate Makefile
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
The value of the variables below will affect the Makefile.
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2005-09-20 19:20:58 -04:00
|
|
|
$CFLAGS: included in CFLAGS make variable (such as -O)
|
|
|
|
$CPPFLAGS: included in CPPFLAGS make variable (such as -I, -D)
|
1999-08-13 01:45:20 -04:00
|
|
|
$LDFLAGS: included in LDFLAGS make variable (such as -L)
|
2005-09-20 19:20:58 -04:00
|
|
|
$objs: list of object file names
|
|
|
|
|
2006-09-13 05:49:58 -04:00
|
|
|
Normally, the object files list is automatically generated by searching
|
|
|
|
source files, but you must define them explicitly if any sources will
|
2005-09-20 19:20:58 -04:00
|
|
|
be generated while building.
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
If a compilation condition is not fulfilled, you should not call
|
2006-09-13 05:49:58 -04:00
|
|
|
``create_makefile''. The Makefile will not be generated, compilation will
|
1999-08-13 01:45:20 -04:00
|
|
|
not be done.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-11-01 05:41:08 -05:00
|
|
|
(5) prepare depend (optional)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
If the file named depend exists, Makefile will include that file to
|
2002-10-23 04:20:35 -04:00
|
|
|
check dependencies. You can make this file by invoking
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-03-05 23:15:42 -05:00
|
|
|
% gcc -MM *.c > depend
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2006-09-13 05:49:58 -04:00
|
|
|
It's harmless. Prepare it.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-11-01 05:41:08 -05:00
|
|
|
(6) generate Makefile
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Try generating the Makefile by:
|
1999-08-13 01:45:20 -04:00
|
|
|
|
|
|
|
ruby extconf.rb
|
|
|
|
|
2007-11-03 09:41:52 -04:00
|
|
|
If the library should be installed under vendor_ruby directory
|
|
|
|
instead of site_ruby directory, use --vendor option as follows.
|
|
|
|
|
|
|
|
ruby extconf.rb --vendor
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
You don't need this step if you put the extension library under the ext
|
1999-08-13 01:45:20 -04:00
|
|
|
directory of the ruby source tree. In that case, compilation of the
|
|
|
|
interpreter will do this step for you.
|
|
|
|
|
2004-11-01 05:41:08 -05:00
|
|
|
(7) make
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Type
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
make
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
to compile your extension. You don't need this step either if you have
|
2006-09-13 05:49:58 -04:00
|
|
|
put the extension library under the ext directory of the ruby source tree.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-11-01 05:41:08 -05:00
|
|
|
(8) debug
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
You may need to rb_debug the extension. Extensions can be linked
|
2006-09-13 05:49:58 -04:00
|
|
|
statically by adding the directory name in the ext/Setup file so that
|
2002-10-23 04:20:35 -04:00
|
|
|
you can inspect the extension with the debugger.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-11-01 05:41:08 -05:00
|
|
|
(9) done, now you have the extension library
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
You can do anything you want with your library. The author of Ruby
|
2002-10-23 04:20:35 -04:00
|
|
|
will not claim any restrictions on your code depending on the Ruby API.
|
1999-08-13 01:45:20 -04:00
|
|
|
Feel free to use, modify, distribute or sell your program.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Appendix A. Ruby source files overview
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
ruby language core
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
class.c : classes and modules
|
|
|
|
error.c : exception classes and exception mechanism
|
|
|
|
gc.c : memory management
|
|
|
|
load.c : library loading
|
|
|
|
object.c : objects
|
|
|
|
variable.c : variables and constants
|
|
|
|
|
|
|
|
ruby syntax parser
|
1998-01-16 07:13:05 -05:00
|
|
|
parse.y
|
2008-09-01 01:08:44 -04:00
|
|
|
-> parse.c : automatically generated
|
|
|
|
keywords : reserved keywords
|
|
|
|
-> lex.c : automatically generated
|
|
|
|
|
|
|
|
ruby evaluator (a.k.a. YARV)
|
|
|
|
compile.c
|
|
|
|
eval.c
|
|
|
|
eval_error.c
|
|
|
|
eval_jump.c
|
|
|
|
eval_safe.c
|
|
|
|
insns.def : definition of VM instructions
|
|
|
|
iseq.c : implementation of VM::ISeq
|
|
|
|
thread.c : thread management and context swiching
|
|
|
|
thread_win32.c : thread implementation
|
|
|
|
thread_pthread.c : ditto
|
|
|
|
vm.c
|
|
|
|
vm_dump.c
|
|
|
|
vm_eval.c
|
2008-11-14 06:31:10 -05:00
|
|
|
vm_exec.c
|
2008-09-01 01:08:44 -04:00
|
|
|
vm_insnhelper.c
|
|
|
|
vm_method.c
|
|
|
|
|
|
|
|
opt_insns_unif.def : instruction unification
|
|
|
|
opt_operand.def : definitions for optimization
|
|
|
|
|
|
|
|
-> insn*.inc : automatically generated
|
|
|
|
-> opt*.inc : automatically generated
|
|
|
|
-> vm.inc : automatically generated
|
|
|
|
|
|
|
|
regular expression engine (oniguruma)
|
|
|
|
regex.c
|
|
|
|
regcomp.c
|
|
|
|
regenc.c
|
|
|
|
regerror.c
|
|
|
|
regexec.c
|
|
|
|
regparse.c
|
|
|
|
regsyntax.c
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
utility functions
|
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
debug.c : debug symbols for C debuggger
|
|
|
|
dln.c : dynamic loading
|
|
|
|
st.c : general purpose hash table
|
|
|
|
strftime.c : formatting times
|
|
|
|
util.c : misc utilities
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
ruby interpreter implementation
|
|
|
|
|
|
|
|
dmyext.c
|
2008-09-01 01:08:44 -04:00
|
|
|
dmydln.c
|
|
|
|
dmyencoding.c
|
|
|
|
id.c
|
1998-01-16 07:13:05 -05:00
|
|
|
inits.c
|
|
|
|
main.c
|
|
|
|
ruby.c
|
|
|
|
version.c
|
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
gem_prelude.rb
|
|
|
|
prelude.rb
|
|
|
|
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
class library
|
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
array.c : Array
|
|
|
|
bignum.c : Bignum
|
|
|
|
compar.c : Comparable
|
|
|
|
complex.c : Complex
|
|
|
|
cont.c : Fiber, Continuation
|
|
|
|
dir.c : Dir
|
|
|
|
enum.c : Enumerable
|
2008-09-01 01:45:06 -04:00
|
|
|
enumerator.c : Enumerator
|
2008-09-01 01:08:44 -04:00
|
|
|
file.c : File
|
|
|
|
hash.c : Hash
|
|
|
|
io.c : IO
|
|
|
|
marshal.c : Marshal
|
|
|
|
math.c : Math
|
|
|
|
numeric.c : Numeric, Integer, Fixnum, Float
|
|
|
|
pack.c : Array#pack, String#unpack
|
|
|
|
proc.c : Binding, Proc
|
|
|
|
process.c : Process
|
|
|
|
random.c : random number
|
|
|
|
range.c : Range
|
|
|
|
rational.c : Rational
|
|
|
|
re.c : Regexp, MatchData
|
|
|
|
signal.c : Signal
|
|
|
|
sprintf.c :
|
|
|
|
string.c : String
|
|
|
|
struct.c : Struct
|
|
|
|
time.c : Time
|
|
|
|
|
2008-09-26 04:34:35 -04:00
|
|
|
defs/knwon_errors.def : Errno::* exception classes
|
|
|
|
-> known_errors.inc : automatically generated
|
|
|
|
|
2008-09-01 01:08:44 -04:00
|
|
|
multilingualization
|
|
|
|
encoding.c : Encoding
|
|
|
|
transcode.c : Encoding::Converter
|
|
|
|
enc/*.c : encoding classes
|
|
|
|
enc/trans/* : codepoint mapping tables
|
|
|
|
|
|
|
|
goruby interpreter implementation
|
|
|
|
|
|
|
|
goruby.c
|
2008-09-26 04:34:35 -04:00
|
|
|
golf_prelude.rb : goruby specific libraries.
|
|
|
|
-> golf_prelude.c : automatically generated
|
2008-09-01 01:08:44 -04:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Appendix B. Ruby extension API reference
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
** Types
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
VALUE
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
The type for the Ruby object. Actual structures are defined in ruby.h,
|
1999-08-13 01:45:20 -04:00
|
|
|
such as struct RString, etc. To refer the values in structures, use
|
|
|
|
casting macros like RSTRING(obj).
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
** Variables and constants
|
|
|
|
|
|
|
|
Qnil
|
|
|
|
|
|
|
|
const: nil object
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Qtrue
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
const: true object(default true value)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Qfalse
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
const: false object
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
** C pointer wrapping
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-24 04:21:56 -04:00
|
|
|
Data_Wrap_Struct(VALUE klass, void (*mark)(), void (*free)(), void *sval)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Wrap a C pointer into a Ruby object. If object has references to other
|
|
|
|
Ruby objects, they should be marked by using the mark function during
|
|
|
|
the GC process. Otherwise, mark should be 0. When this object is no
|
|
|
|
longer referred by anywhere, the pointer will be discarded by free
|
|
|
|
function.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-24 04:21:56 -04:00
|
|
|
Data_Make_Struct(klass, type, mark, free, sval)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
This macro allocates memory using malloc(), assigns it to the variable
|
|
|
|
sval, and returns the DATA encapsulating the pointer to memory region.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
Data_Get_Struct(data, type, sval)
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
This macro retrieves the pointer value from DATA, and assigns it to
|
|
|
|
the variable sval.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-03-20 01:27:22 -05:00
|
|
|
** Checking data types
|
|
|
|
|
|
|
|
TYPE(value)
|
|
|
|
FIXNUM_P(value)
|
|
|
|
NIL_P(value)
|
|
|
|
void Check_Type(VALUE value, int type)
|
|
|
|
void Check_SafeStr(VALUE value)
|
|
|
|
|
|
|
|
** Data type conversion
|
|
|
|
|
2009-05-09 01:04:25 -04:00
|
|
|
FIX2INT(value), INT2FIX(i)
|
|
|
|
FIX2LONG(value), LONG2FIX(l)
|
|
|
|
NUM2INT(value), INT2NUM(i)
|
|
|
|
NUM2UINT(value), UINT2NUM(ui)
|
|
|
|
NUM2LONG(value), LONG2NUM(l)
|
|
|
|
NUM2ULONG(value), ULONG2NUM(ul)
|
|
|
|
NUM2LL(value), LL2NUM(ll)
|
|
|
|
NUM2ULL(value), ULL2NUM(ull)
|
|
|
|
NUM2OFFT(value), OFFT2NUM(off)
|
|
|
|
NUM2SIZET(value), SIZET2NUM(size)
|
|
|
|
NUM2SSIZET(value), SSIZET2NUM(ssize)
|
2003-03-20 01:27:22 -05:00
|
|
|
NUM2DBL(value)
|
|
|
|
rb_float_new(f)
|
2005-09-20 19:20:58 -04:00
|
|
|
StringValue(value)
|
|
|
|
StringValuePtr(value)
|
|
|
|
StringValueCStr(value)
|
2003-03-20 01:27:22 -05:00
|
|
|
rb_str_new2(s)
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
** defining class/module
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
VALUE rb_define_class(const char *name, VALUE super)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Defines a new Ruby class as a subclass of super.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
VALUE rb_define_class_under(VALUE module, const char *name, VALUE super)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Creates a new Ruby class as a subclass of super, under the module's
|
1999-08-13 01:45:20 -04:00
|
|
|
namespace.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
VALUE rb_define_module(const char *name)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Defines a new Ruby module.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2004-04-26 10:56:58 -04:00
|
|
|
VALUE rb_define_module_under(VALUE module, const char *name)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Defines a new Ruby module under the module's namespace.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-24 04:21:56 -04:00
|
|
|
void rb_include_module(VALUE klass, VALUE module)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Includes module into class. If class already includes it, just
|
2002-10-23 04:20:35 -04:00
|
|
|
ignored.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
void rb_extend_object(VALUE object, VALUE module)
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Extend the object with the module's attributes.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
** Defining Global Variables
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_variable(const char *name, VALUE *var)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Defines a global variable which is shared between C and Ruby. If name
|
2002-10-23 04:20:35 -04:00
|
|
|
contains a character which is not allowed to be part of the symbol,
|
1999-01-19 23:59:39 -05:00
|
|
|
it can't be seen from Ruby programs.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_readonly_variable(const char *name, VALUE *var)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Defines a read-only global variable. Works just like
|
2006-09-13 05:49:58 -04:00
|
|
|
rb_define_variable(), except the defined variable is read-only.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_virtual_variable(const char *name,
|
2000-03-05 23:15:42 -05:00
|
|
|
VALUE (*getter)(), VALUE (*setter)())
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Defines a virtual variable, whose behavior is defined by a pair of C
|
1999-01-19 23:59:39 -05:00
|
|
|
functions. The getter function is called when the variable is
|
2006-09-13 05:49:58 -04:00
|
|
|
referenced. The setter function is called when the variable is set to a
|
|
|
|
value. The prototype for getter/setter functions are:
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
VALUE getter(ID id)
|
|
|
|
void setter(VALUE val, ID id)
|
|
|
|
|
|
|
|
The getter function must return the value for the access.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_hooked_variable(const char *name, VALUE *var,
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE (*getter)(), VALUE (*setter)())
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Defines hooked variable. It's a virtual variable with a C variable.
|
|
|
|
The getter is called as
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
VALUE getter(ID id, VALUE *var)
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
returning a new value. The setter is called as
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
void setter(VALUE val, ID id, VALUE *var)
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
GC requires C global variables which hold Ruby values to be marked.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
void rb_global_variable(VALUE *var)
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Tells GC to protect these variables.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
** Constant Definition
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_const(VALUE klass, const char *name, VALUE val)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Defines a new constant under the class/module.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_define_global_const(const char *name, VALUE val)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Defines a global constant. This is just the same as
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
rb_define_const(cKernal, name, val)
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
** Method Definition
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
rb_define_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Defines a method for the class. func is the function pointer. argc
|
|
|
|
is the number of arguments. if argc is -1, the function will receive
|
2002-10-23 04:20:35 -04:00
|
|
|
3 arguments: argc, argv, and self. if argc is -2, the function will
|
|
|
|
receive 2 arguments, self and args, where args is a Ruby array of
|
1999-08-13 01:45:20 -04:00
|
|
|
the method arguments.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Defines a private method for the class. Arguments are same as
|
|
|
|
rb_define_method().
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
rb_define_singleton_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Defines a singleton method. Arguments are same as rb_define_method().
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
rb_scan_args(int argc, VALUE *argv, const char *fmt, ...)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2009-02-18 13:34:38 -05:00
|
|
|
Retrieve argument from argc and argv to given VALUE references
|
|
|
|
according to the format string. The format can be described in ABNF
|
|
|
|
as follows:
|
|
|
|
|
|
|
|
--
|
2010-09-10 03:51:58 -04:00
|
|
|
scan-arg-spec := param-arg-spec [option-hash-arg-spec] [block-arg-spec]
|
2009-02-18 13:34:38 -05:00
|
|
|
|
2009-02-24 10:41:39 -05:00
|
|
|
param-arg-spec := pre-arg-spec [post-arg-spec] / post-arg-spec / pre-opt-post-arg-spec
|
2009-02-18 13:34:38 -05:00
|
|
|
pre-arg-spec := num-of-leading-mandatory-args [num-of-optional-args]
|
|
|
|
post-arg-spec := sym-for-variable-length-args [num-of-trailing-mandatory-args]
|
2009-02-24 10:41:39 -05:00
|
|
|
pre-opt-post-arg-spec := num-of-leading-mandatory-args num-of-optional-args num-of-trailing-mandatory-args
|
2010-09-10 03:51:58 -04:00
|
|
|
option-hash-arg-spec := sym-for-option-hash-arg
|
2009-02-18 13:34:38 -05:00
|
|
|
block-arg-spec := sym-for-block-arg
|
|
|
|
|
|
|
|
num-of-leading-mandatory-args := DIGIT ; The number of leading
|
|
|
|
; mandatory arguments
|
|
|
|
num-of-optional-args := DIGIT ; The number of optional
|
|
|
|
; arguments
|
|
|
|
sym-for-variable-length-args := "*" ; Indicates that variable
|
|
|
|
; length arguments are
|
|
|
|
; captured as a ruby array
|
|
|
|
num-of-trailing-mandatory-args := DIGIT ; The number of trailing
|
|
|
|
; mandatory arguments
|
2010-09-10 03:51:58 -04:00
|
|
|
sym-for-option-hash-arg := ":" ; Indicates that an option
|
|
|
|
; hash is captured if the last
|
|
|
|
; argument is a hash or can be
|
|
|
|
; converted to a hash with
|
|
|
|
; #to_hash. When the last
|
|
|
|
; argument is nil, it is
|
|
|
|
; captured if it is not
|
|
|
|
; ambiguous to take it as
|
|
|
|
; empty option hash; i.e. '*'
|
|
|
|
; is not specified and
|
|
|
|
; arguments are given more
|
|
|
|
; than sufficient.
|
2009-02-18 13:34:38 -05:00
|
|
|
sym-for-block-arg := "&" ; Indicates that an iterator
|
|
|
|
; block should be captured if
|
|
|
|
; given
|
|
|
|
--
|
|
|
|
|
|
|
|
For example, "12" means that the method requires at least one
|
|
|
|
argument, and at most receives three (1+2) arguments. So, the format
|
|
|
|
string must be followed by three variable references, which are to be
|
|
|
|
assigned to captured arguments. For omitted arguments, variables are
|
|
|
|
set to Qnil. NULL can be put in place of a variable reference, which
|
|
|
|
means the corresponding captured argument(s) should be just dropped.
|
|
|
|
|
2010-09-10 03:51:58 -04:00
|
|
|
The number of given arguments, excluding an option hash or iterator
|
|
|
|
block, is returned.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
** Invoking Ruby method
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
VALUE rb_funcall(VALUE recv, ID mid, int narg, ...)
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Invokes a method. To retrieve mid from a method name, use rb_intern().
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
VALUE rb_funcall2(VALUE recv, ID mid, int argc, VALUE *argv)
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Invokes a method, passing arguments by an array of values.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
VALUE rb_eval_string(const char *str)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Compiles and executes the string as a Ruby program.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
ID rb_intern(const char *name)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Returns ID corresponding to the name.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
char *rb_id2name(ID id)
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Returns the name corresponding ID.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-24 04:21:56 -04:00
|
|
|
char *rb_class2name(VALUE klass)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Returns the name of the class.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-03-05 23:15:42 -05:00
|
|
|
int rb_respond_to(VALUE object, ID id)
|
1999-10-12 00:53:36 -04:00
|
|
|
|
2000-05-01 05:42:38 -04:00
|
|
|
Returns true if the object responds to the message specified by id.
|
1999-10-12 00:53:36 -04:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
** Instance Variables
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
VALUE rb_iv_get(VALUE obj, const char *name)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Retrieve the value of the instance variable. If the name is not
|
|
|
|
prefixed by `@', that variable shall be inaccessible from Ruby.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Sets the value of the instance variable.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
** Control Structure
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2009-07-13 12:07:43 -04:00
|
|
|
VALUE rb_block_call(VALUE recv, ID mid, int argc, VALUE * argv,
|
|
|
|
VALUE (*func) (ANYARGS), VALUE data2)
|
|
|
|
|
|
|
|
Calls a method on the recv, with the method name specified by the
|
2010-07-11 08:20:28 -04:00
|
|
|
symbol mid, with argc arguments in argv, supplying func as the
|
|
|
|
block. When func is called as the block, it will receive the value
|
|
|
|
from yield as the first argument, and data2 as the second argument.
|
|
|
|
When yielded with multiple values (in C, rb_yield_values(),
|
|
|
|
rb_yield_values2() and rb_yield_splat()), data2 is packed as an Array,
|
|
|
|
whereas yielded values can be gotten via argc/argv of the third/fourth
|
|
|
|
arguments.
|
2009-07-13 12:07:43 -04:00
|
|
|
|
|
|
|
[OBSOLETE] VALUE rb_iterate(VALUE (*func1)(), void *arg1, VALUE (*func2)(), void *arg2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Calls the function func1, supplying func2 as the block. func1 will be
|
|
|
|
called with the argument arg1. func2 receives the value from yield as
|
|
|
|
the first argument, arg2 as the second argument.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2009-07-13 12:07:43 -04:00
|
|
|
When rb_iterate is used in 1.9, func1 has to call some Ruby-level method.
|
|
|
|
This function is obsolete since 1.9; use rb_block_call instead.
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE rb_yield(VALUE val)
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Evaluates the block with value val.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2010-07-21 14:03:34 -04:00
|
|
|
VALUE rb_rescue(VALUE (*func1)(), VALUE arg1, VALUE (*func2)(), VALUE arg2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Calls the function func1, with arg1 as the argument. If an exception
|
1999-08-13 01:45:20 -04:00
|
|
|
occurs during func1, it calls func2 with arg2 as the argument. The
|
|
|
|
return value of rb_rescue() is the return value from func1 if no
|
|
|
|
exception occurs, from func2 otherwise.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2010-07-21 14:03:34 -04:00
|
|
|
VALUE rb_ensure(VALUE (*func1)(), VALUE arg1, VALUE (*func2)(), VALUE arg2)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
Calls the function func1 with arg1 as the argument, then calls func2
|
2002-10-23 04:20:35 -04:00
|
|
|
with arg2 if execution terminated. The return value from
|
2009-09-15 21:14:56 -04:00
|
|
|
rb_ensure() is that of func1 when no exception occured.
|
|
|
|
|
|
|
|
VALUE rb_protect(VALUE (*func) (VALUE), VALUE arg, int *state)
|
|
|
|
|
|
|
|
Calls the function func with arg as the argument. If no exception
|
|
|
|
occured during func, it returns the result of func and *state is zero.
|
|
|
|
Otherwise, it returns Qnil and sets *state to nonzero. If state is
|
|
|
|
NULL, it is not set in both cases.
|
|
|
|
|
|
|
|
void rb_jump_tag(int state)
|
|
|
|
|
|
|
|
Continues the exception caught by rb_protect() and rb_eval_string_protect().
|
|
|
|
state must be the returned value from those functions. This function
|
|
|
|
never return to the caller.
|
1999-08-13 01:45:20 -04:00
|
|
|
|
|
|
|
** Exceptions and Errors
|
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_warn(const char *fmt, ...)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Prints a warning message according to a printf-like format.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_warning(const char *fmt, ...)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Prints a warning message according to a printf-like format, if
|
1999-08-13 01:45:20 -04:00
|
|
|
$VERBOSE is true.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-03-20 01:27:22 -05:00
|
|
|
void rb_raise(rb_eRuntimeError, const char *fmt, ...)
|
|
|
|
|
|
|
|
Raises RuntimeError. The fmt is a format string just like printf().
|
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_raise(VALUE exception, const char *fmt, ...)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Raises a class exception. The fmt is a format string just like printf().
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_fatal(const char *fmt, ...)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Raises a fatal error, terminates the interpreter. No exception handling
|
|
|
|
will be done for fatal errors, but ensure blocks will be executed.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-10-05 05:57:04 -04:00
|
|
|
void rb_bug(const char *fmt, ...)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-24 04:21:56 -04:00
|
|
|
Terminates the interpreter immediately. This function should be
|
1999-08-13 01:45:20 -04:00
|
|
|
called under the situation caused by the bug in the interpreter. No
|
|
|
|
exception handling nor ensure execution will be done.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2006-09-13 05:49:58 -04:00
|
|
|
** Initialize and Start the Interpreter
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
The embedding API functions are below (not needed for extension libraries):
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-10-04 00:51:08 -04:00
|
|
|
void ruby_init()
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Initializes the interpreter.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-10-04 00:51:08 -04:00
|
|
|
void ruby_options(int argc, char **argv)
|
|
|
|
|
|
|
|
Process command line arguments for the interpreter.
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
void ruby_run()
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Starts execution of the interpreter.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
void ruby_script(char *name)
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Specifies the name of the script ($0).
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2005-03-16 08:05:46 -05:00
|
|
|
** Hooks for the Interpreter Events
|
|
|
|
|
2009-05-20 23:07:45 -04:00
|
|
|
void rb_add_event_hook(rb_event_hook_func_t func, rb_event_flag_t events, VALUE data)
|
2005-03-16 08:05:46 -05:00
|
|
|
|
|
|
|
Adds a hook function for the specified interpreter events.
|
|
|
|
events should be Or'ed value of:
|
|
|
|
|
|
|
|
RUBY_EVENT_LINE
|
|
|
|
RUBY_EVENT_CLASS
|
|
|
|
RUBY_EVENT_END
|
|
|
|
RUBY_EVENT_CALL
|
|
|
|
RUBY_EVENT_RETURN
|
|
|
|
RUBY_EVENT_C_CALL
|
|
|
|
RUBY_EVENT_C_RETURN
|
|
|
|
RUBY_EVENT_RAISE
|
|
|
|
RUBY_EVENT_ALL
|
|
|
|
|
|
|
|
The definition of rb_event_hook_func_t is below:
|
|
|
|
|
2009-05-20 23:07:45 -04:00
|
|
|
typedef void (*rb_event_hook_func_t)(rb_event_t event, VALUE data,
|
2005-03-16 08:05:46 -05:00
|
|
|
VALUE self, ID id, VALUE klass)
|
|
|
|
|
2009-05-20 23:07:45 -04:00
|
|
|
The third argument `data' to rb_add_event_hook() is passed to the hook
|
|
|
|
function as the second argument, which was the pointer to the current
|
|
|
|
NODE in 1.8. See RB_EVENT_HOOKS_HAVE_CALLBACK_DATA below.
|
|
|
|
|
2005-03-16 08:05:46 -05:00
|
|
|
int rb_remove_event_hook(rb_event_hook_func_t func)
|
|
|
|
|
|
|
|
Removes the specified hook function.
|
|
|
|
|
2009-02-22 20:15:37 -05:00
|
|
|
** Macros for the Compatibilities
|
|
|
|
|
|
|
|
Some macros to check API compatibilities are available by default.
|
|
|
|
|
|
|
|
NORETURN_STYLE_NEW
|
|
|
|
|
|
|
|
Means that NORETURN macro is functional style instead of prefix.
|
|
|
|
|
|
|
|
HAVE_RB_DEFINE_ALLOC_FUNC
|
|
|
|
|
|
|
|
Means that function rb_define_alloc_func() is provided, that means the
|
|
|
|
allocation framework is used. This is same as the result of
|
|
|
|
have_func("rb_define_alloc_func", "ruby.h").
|
|
|
|
|
|
|
|
HAVE_RB_REG_NEW_STR
|
|
|
|
|
|
|
|
Means that function rb_reg_new_str() is provided, that creates Regexp
|
|
|
|
object from String object. This is same as the result of
|
|
|
|
have_func("rb_reg_new_str", "ruby.h").
|
|
|
|
|
|
|
|
HAVE_RB_IO_T
|
|
|
|
|
|
|
|
Means that type rb_io_t is provided.
|
|
|
|
|
2009-02-22 20:19:43 -05:00
|
|
|
USE_SYMBOL_AS_METHOD_NAME
|
|
|
|
|
|
|
|
Means that Symbols will be returned as method names, e.g.,
|
|
|
|
Module#methods, #singleton_methods and so on.
|
|
|
|
|
2009-03-02 02:40:13 -05:00
|
|
|
HAVE_RUBY_*_H
|
|
|
|
|
|
|
|
Defined in ruby.h and means correspoinding header is available. For
|
|
|
|
instance, when HAVE_RUBY_ST_H is defined you should use ruby/st.h not
|
|
|
|
mere st.h.
|
2009-02-22 20:15:37 -05:00
|
|
|
|
2009-05-20 23:07:45 -04:00
|
|
|
RB_EVENT_HOOKS_HAVE_CALLBACK_DATA
|
|
|
|
|
|
|
|
Means that rb_add_event_hook() takes the third argument `data', to be
|
|
|
|
passed to the given event hook function.
|
|
|
|
|
2001-11-29 10:30:54 -05:00
|
|
|
Appendix C. Functions Available in extconf.rb
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
These functions are available in extconf.rb:
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2005-03-31 08:24:27 -05:00
|
|
|
have_macro(macro, headers)
|
|
|
|
|
|
|
|
Checks whether macro is defined with header. Returns true if the macro
|
|
|
|
is defined.
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
have_library(lib, func)
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Checks whether the library exists, containing the specified function.
|
1999-01-19 23:59:39 -05:00
|
|
|
Returns true if the library exists.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-05-10 14:38:11 -04:00
|
|
|
find_library(lib, func, path...)
|
|
|
|
|
2002-10-23 04:20:35 -04:00
|
|
|
Checks whether a library which contains the specified function exists in
|
2001-05-10 14:38:11 -04:00
|
|
|
path. Returns true if the library exists.
|
|
|
|
|
2000-07-06 23:20:53 -04:00
|
|
|
have_func(func, header)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-06 23:20:53 -04:00
|
|
|
Checks whether func exists with header. Returns true if the function
|
2002-10-23 04:20:35 -04:00
|
|
|
exists. To check functions in an additional library, you need to
|
2005-03-31 08:24:27 -05:00
|
|
|
check that library first using have_library().
|
|
|
|
|
|
|
|
have_var(var, header)
|
|
|
|
|
|
|
|
Checks whether var exists with header. Returns true if the variable
|
|
|
|
exists. To check variables in an additional library, you need to
|
2000-07-06 23:20:53 -04:00
|
|
|
check that library first using have_library().
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
have_header(header)
|
|
|
|
|
2001-05-10 14:38:11 -04:00
|
|
|
Checks whether header exists. Returns true if the header file exists.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2005-03-31 08:24:27 -05:00
|
|
|
find_header(header, path...)
|
|
|
|
|
|
|
|
Checks whether header exists in path. Returns true if the header file
|
|
|
|
exists.
|
|
|
|
|
|
|
|
have_struct_member(type, member, header)
|
|
|
|
|
|
|
|
Checks whether type has member with header. Returns true if the type
|
|
|
|
is defined and has the member.
|
|
|
|
|
|
|
|
have_type(type, header, opt)
|
|
|
|
|
|
|
|
Checks whether type is defined with header. Returns true if the type
|
|
|
|
is defined.
|
|
|
|
|
|
|
|
check_sizeof(type, header)
|
|
|
|
|
|
|
|
Checks the size of type in char with header. Returns the size if the
|
|
|
|
type is defined, otherwise nil.
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
create_makefile(target)
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
Generates the Makefile for the extension library. If you don't invoke
|
|
|
|
this method, the compilation will not be done.
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2005-03-31 08:24:27 -05:00
|
|
|
find_executable(bin, path)
|
|
|
|
|
|
|
|
Finds command in path, which is File::PATH_SEPARATOR-separated list of
|
2007-12-23 16:01:25 -05:00
|
|
|
directories. If path is nil or omitted, environment variable PATH
|
2005-03-31 08:24:27 -05:00
|
|
|
will be used. Returns the path name of the command if it is found,
|
|
|
|
otherwise nil.
|
|
|
|
|
2001-05-10 14:38:11 -04:00
|
|
|
with_config(withval[, default=nil])
|
|
|
|
|
|
|
|
Parses the command line options and returns the value specified by
|
|
|
|
--with-<withval>.
|
|
|
|
|
2005-03-31 08:24:27 -05:00
|
|
|
enable_config(config, *defaults)
|
|
|
|
disable_config(config, *defaults)
|
|
|
|
|
|
|
|
Parses the command line options for boolean. Returns true if
|
|
|
|
--enable-<config> is given, or false if --disable-<config> is given.
|
|
|
|
Otherwise, yields defaults to the given block and returns the result
|
|
|
|
if it is called with a block, or returns defaults.
|
|
|
|
|
2001-05-10 14:38:11 -04:00
|
|
|
dir_config(target[, default_dir])
|
|
|
|
dir_config(target[, default_include, default_lib])
|
|
|
|
|
|
|
|
Parses the command line options and adds the directories specified by
|
|
|
|
--with-<target>-dir, --with-<target>-include, and/or --with-<target>-lib
|
|
|
|
to $CFLAGS and/or $LDFLAGS. --with-<target>-dir=/path is equivalent to
|
|
|
|
--with-<target>-include=/path/include --with-<target>-lib=/path/lib.
|
|
|
|
Returns an array of the added directories ([include_dir, lib_dir]).
|
|
|
|
|
2005-03-31 08:24:27 -05:00
|
|
|
pkg_config(pkg)
|
|
|
|
|
2006-09-13 05:49:58 -04:00
|
|
|
Obtains the information for pkg by pkg-config command. The actual
|
2007-12-23 16:01:25 -05:00
|
|
|
command name can be overridden by --with-pkg-config command line
|
2005-03-31 08:24:27 -05:00
|
|
|
option.
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
/*
|
|
|
|
* Local variables:
|
1999-08-13 01:45:20 -04:00
|
|
|
* fill-column: 70
|
1998-01-16 07:13:05 -05:00
|
|
|
* end:
|
|
|
|
*/
|