Subject: embeddig ruby in a C application
	Date: Fri 27 Mar 15 07:30:03PM +0100

Quoting mortee (mortee.lists / kavemalna.hu):

> I learned about MRuby, which seems pretty cool, and I was able to run some
> hello world code using it, but it's really undocumented. I was unable to do
> as much as get a string ruby constant's value as a C string - so
> communication between the two worlds needs some explanation, as well as how
> one can have a more complete ruby environment (standard lib, require and
> stuff).

I have been developing exclusively with Mruby+C for the past four
months. With great satisfaction. It is true that there is not too much
in the field of documentation, but the code is well-written, and there
are in it more than enough practical examples to help you learn.

I do not understand precisely what you mean by "a string ruby
constant's value". You pass parameters from Ruby to C by defining
methods this way:

mrb_define_method(mrb,cls,"method_name",method_function,MRB_ARGS_REQ(1));

(in this example you specify you will pass exactly one parameter to
the method). Then, inside your method, you obtain the parameters with
a call to 'mrb_get_args.' A little bit of info can be found in the
doc/api/mruby.h.md file.

For each parameter to be received into C, one letter is included in
the second parameter to mrb_get_args. There are three ways to receive
strings (instances of String class) from Mruby to C. If you include a
'S' (capital es), you assign your string to a mrb_value (common Mruby
object). For example:

mrb_value str;
mrb_get_args(mrb,"S",&str);

Then, you handle this mrb_value with the same macros you can use in
MRI:

RSTRING_LEN(str) returns the length of the string
RSTRING_PTR(str) returns a pointer to the string's contents

If you use a lowercase 's', you will then pass to mrb_get_args two
parameters:

int str_len;
char *str;
mrb_get_args(mrb,"s",&str,&len);

str then contains the pointer to the string's data, while len contains
the length.

You can also use a lowercase 'z'. This way, the string will be
received as a 'C' (i.e. zero-terminated) string:

char *str;
mrb_get_args(mrb,"z",&str);

Just search into the Mruby code for examples of mrb_define_method and
mrb_get_args. There are many.

HTH

Carlo

-- 
  *         Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido / fluido.as             che bisogno ci sarebbe
  *               di parlare tanto di amore e di rettitudine? (Chuang-Tzu)