1

I am trying to convert some of the ruby interpreter code called in C to mruby format. I am stuck and would appreciate help here.

My testruby.rb file content:

#require 'MyMod'

def helloworld(var1)
    puts "You said #{var1}"
    return MyMod.Issue1(var1).to_s
end

Below is the snippet of my C++ file:

Issue 1:

static mrb_value Issue1(mrb_state *mrb, mrb_value mrb_self)
{
    mrb_??? val1; // What should be the type for string and where to find all the types?
    mrb_get_args(mrb, "s", ?);
// How to manipulate val1? Say I want to concatenate few more data.
    return mrb_????(val1); // How do I return this value?
} 

The above method, I am sending as a module to the mruby interpreter so that .rb file can call this.

Please let me know if below format is the correct one:

struct RClass *mod = mrb_define_module(mrb, "MyMod");
mrb_define_module_function(mrb, mod, "SumI", Issue1, MRB_ARGS_REQ(1));

Issue2:

How do I convert the below ruby interpreter code to mruby?

rb_require("./testruby"); // where testruby is my testruby.rb file

Now I want to call the helloworld method from testruby.rb file. How do I call the equivalent method for mruby (for rb_funcall)?

How do I read the return value from the helloworld method in my c++ code?

Regards,

programmer
  • 582
  • 2
  • 4
  • 16

1 Answers1

2

Re val1: mrb_value is the type that can hold any mruby object Manipulating val1 could be done using mrb_funcall. That function returns a mrb_value:

mrb_value my_str = mrb_funcall(mrb_context, your_object, "your_method", 0);
printf("my_str = %s\n", RSTRING_PTR(my_str));

Re issue 2: There's no require in mruby: mrbgems are compiled and linked statically with the target binary (they are listed in the top-level build_config.rb file). (A gem called mruby-require exists to mimic CRuby's require, but I've never used it)

Franck Verrot
  • 1,041
  • 7
  • 9