Podrška #15094
Zatvorenruby c extensions
0%
Fajlovi
Povezani tiketi 1 (0 otvoreno — 1 zatvoren)
Izmjenjeno od Ernad Husremović prije skoro 18 godina
Arrays¶
Handling arrays is just as easy, but it requires an extra data structures, struct RArray. Again, there is a simple macro for converting a VALUE that stores an array to an RArray pointer: RARRAY. It's use is demonstrated in the example above:
struct RArray *names_array; <<<<<<<<<< names_array koristimo u C-u names_array = RARRAY(names); <<<<<<<<< names je ruby matrica
The RArray structure has two important fields: len and ptr. len is the number of elements in the array, while ptr is the pointer to the VALUEs that are stored inside the array. Pretty straight forward, isn't it? Here's some code that shows how you can iterate over an array:
for(i = 0; i < names_array->len; i++)
{
VALUE current = names_array->ptr[i]; <<<< current elemenat matrice
...
}
Izmjenjeno od Ernad Husremović prije skoro 18 godina
http://www.idle-hacking.com/2008/05/ruby-extension-memory-leak-tracking/
Using a valgrind and a nice patch for ruby 1.8.6, I now have rb-brill-tagger leak free.
Here’s the process I went through. First off you need a linux environment to run valgrind. If you don’t already have one setup, I recommend fedora core. It’s super easy to run and has a pretty good track record with hardward. Also, yum is super easy to use to install new software.
Izmjenjeno od Ernad Husremović prije skoro 18 godina
http://www.rubyist.net/~nobu/ruby/Ruby_Extension_Manual.html
method arguments¶
There are three kinds of way to receive method arguments.
method 1: fixed number of arguments¶
First, methods with a fixed number of arguments receive arguments like this:
static VALUE
fdbm_delete(obj, keystr)
VALUE obj, keystr;
{
:
}
The first argument of the C function is the self, the rest are the arguments to the method. Second, methods with an arbitrary number of arguments receive arguments like this:
method 2: argc, argv, klass - rb_scan_args¶
static VALUE
fdbm_s_open(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
{
:
if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
mode = 0666; /* default value */
}
:
}
The first argument is the number of method arguments, the second argument is the C array of the method arguments, and the third argument is the receiver of the method.
You can use the function rb_scan_args() to check and retrieve the arguments. For example, "11" means that the method requires at least one argument, and at most receives two arguments.
method 3: obj, args - ruby array¶
Methods with an arbitrary number of arguments can receive arguments by Ruby's array, like this:
static VALUE
fdbm_indexes(obj, args)
VALUE obj, args;
{
:
}
The first argument is the receiver, the second one is the Ruby array which contains the arguments to the method.
Izmjenjeno od Ernad Husremović prije više od 17 godina
- Status promijenjeno iz Dodijeljeno u Zatvoreno