c - 在 C 语言中扩展 Ruby - 将参数转换为 C 类型

标签 c ruby node.js ruby-c-extension

我如何为 ruby​​ 做类似的事情。我无法找到将变量转换为对象的示例/文档。例如

Local<Object> obj = args[0]->ToObject();
Local<Array> props = obj->GetPropertyNames();

我正在用 ruby​​ 重写 Node 扩展。任何形式的帮助都会非常有帮助。谢谢

static Handle<Value> SendEmail(const Arguments& args)
{
    HandleScope scope;

    list<string> values;
    Local<Object> obj = args[0]->ToObject();
    Local<Array> props = obj->GetPropertyNames();

    // Iterate through args[0], adding each element to our list
    for(unsigned int i = 0; i < props->Length(); i++) {
        String::AsciiValue val(obj->Get(i)->ToString());
        values.push_front(string(*val));
    }

    // Display the values in the list for debugging purposes
    for (list<string>::iterator it = values.begin(); it != values.end(); it++) {
        cout << *it << endl;
    }

    return scope.Close(args.This());
}

最佳答案

我不完全确定我理解你的问题,因为你将此问题标记为 C 并且你的代码示例是 C++,但无论哪种方式,如果你尝试扩展 ruby​​,你都需要将 ruby​​ 值转换为C 类型,如果您计划使用 C++ 数据结构,则从 C 类型到 C++ 对象。

使用 Ruby C Api:

#include "ruby.h"
//Define module my_module (this can be any name but it should be the name of your extension
VALUE MyModule = Qnil;
//Initialization prototype - required by ruby
void Init_MyModule();
//method prototype declarations - all methods must be prefaced by method_
VALUE method_get_some_ruby_value();

//actual method
VALUE get_some_ruby_value(VALUE self, VALUE some_value)
{
    //If your value is a ruby string - the StringValue function from ruby will ensure 
    //that you get a string no matter what, even if the type being passed is not a string.
    VALUE r_string_value = StringValue(some_value);

    //Now we tell the Ruby API to give us the pointer to the ruby string 
    //value and we assign that to a native char * in C. 
    char *c_string_value = RSTRING_PTR(r_string_value);

    //Now do something with the char * - for example, turn it into a C++ string
    std::string str(c_string_value);
    // ...do something useful here...

    return Qnil; // If you want to return something back to ruby, you can return a VALUE.   
}
//Ruby calls this to initialize your module in ruby
VALUE Init_MyModule()
{   
    //This defines your module in ruby
    MyModule = rb_define_module("MyModule");
    //This defines the method - the module it is in, the ruby method name, the actual method ruby will call, and the number of arguments this function is expected to take
    rb_define_method(MyModule, "get_some_ruby_value", get_some_ruby_value, 1);
}

如果您想了解如何在 Ruby/C/C++ 之间来回传递结构,则在开始尝试使用 C++ 对象之前,您需要彻底了解 Ruby 和 Ruby C API。

这里有一些可以帮助您入门的优秀资源:

http://silverhammermba.github.io/emberb/c/ http://java.ociweb.com/mark/NFJS/RubyCExtensions.pdf

关于c - 在 C 语言中扩展 Ruby - 将参数转换为 C 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27234317/

相关文章:

创建和使用字体/避免 Windows GDI 中的内存泄漏

c - 在没有for的情况下向左旋转C中的数组?

c - 理解 C 中的指针和堆栈

c - strcpy() 不写入新字符串

ruby - 如何在 Sinatra 中编写部分 ERB 模板?

ruby-on-rails - Ruby Rails 在返回多个对象的查询中平均两个属性

node.js - 有什么充分的理由让您的应用程序为 node.js 调用 `process.exit()` 吗?

ruby-on-rails - 如何在 ruby​​ 中动态调用关联?

javascript - 如何使用botkit在特定时间发送消息?

Node.js/Express.js : Factory design pattern for DB queries