简体   繁体   English

C中的Ruby添加到$ LOAD_PATH

[英]Ruby inside C adding to $LOAD_PATH

Effectively I want to know if I can, purely through the use of Ruby's C library, append to $LOAD_PATH. 实际上,我想知道是否可以(仅通过使用Ruby的C库)附加到$ LOAD_PATH。 The reason for doing so is that I have a written an extension (using Rice but that's not super important) and I would like for it to be self contained with a few others in their own directory. 这样做的原因是,我编写了一个扩展名(使用Rice,但这并不重要),我希望它与其他几个文件一起包含在自己的目录中。

Now, I already have two working solutions that I'm fine with. 现在,我已经有了两个可行的解决方案。 The first being that I simply use the Makefile generated by Rice to install the shared object automatically into a standard directory that is already on $LOAD_PATH. 首先,我只是使用Rice生成的Makefile将共享库自动安装到已经在$ LOAD_PATH上的标准目录中。 Super easy no hassle. 超级容易,无烦恼。 The other is that I export $RUBY_LIB as the directory I want before running and the Ruby runtime picks up on that like a champ. 另一个是我在运行之前将$ RUBY_LIB导出为我想要的目录,而Ruby运行时则像冠军一样接手了。 But what I want to know is if I can do it only in C - for reference I am looking for functionality that mimics ruby -I./somedir 但是我想知道的是,我是否只能在C中做到这一点-供参考,我正在寻找模仿ruby -I./somedir

Right now I'm initialising Ruby in C the following way, this works fine with the previously mentioned working solutions but what I want is a way to cleanly add a directory to Ruby's $LOAD_PATH at runtime. 现在,我正在按照以下方式用C初始化Ruby,这与前面提到的工作解决方案可以很好地工作,但是我想要的是一种在运行时将目录干净地添加到Ruby的$ LOAD_PATH的方法。

ruby_sysinit(&argc, &argv);
RUBY_INIT_STACK;
ruby_init();
ruby_init_loadpath();
....
rb_load_protect(...)
rb_funcall(...)

I could not figure out how to work with ruby_options , it just gave me a node and then blocked the main thread so I couldn't do anything; 我不知道如何使用ruby_options ,它只是给了我一个节点,然后阻塞了主线程,所以我什么也做不了。 was I using it wrong? 我使用错了吗?

Thanks! 谢谢!

To access the load the $LOAD_PATH variable, use the rb_gv_get("$LOAD_PATH") in your code. 要访问负载$LOAD_PATH变量,请在代码中使用rb_gv_get("$LOAD_PATH")

The rb_gv_get("$LOAD_PATH") returns a Ruby array object, so any C array function can be used, such as rb_ary_unshift , rb_ary_push , etc. rb_gv_get("$LOAD_PATH")返回一个Ruby数组对象,因此可以使用任何C数组函数,例如rb_ary_unshiftrb_ary_push等。

For example: 例如:

VALUE load_path = rb_gv_get("$LOAD_PATH");
/* add a directory to Ruby's $LOAD_PATH */
rb_ary_push(load_path, rb_str_new2("any expanded directory"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM