0

How I can do similar for ruby. I am not able to find a example/documentation for casting a variable to object. For example

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

I am re-writing a node extension in ruby. Any sort of help will be very helpful. Thanks

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());
}
3
  • Did you try a Google search: "typecasting in Ruby". Commented Dec 1, 2014 at 18:11
  • Yeah. Unfortunately not able to get useful information Commented Dec 1, 2014 at 19:52
  • because ruby is not strongly typed, there is no notion of "typecasting" in ruby - it is what it is... you may want to transform and object, or convert it, but casting is meaningless Commented Dec 2, 2014 at 7:27

1 Answer 1

2

I'm not entirely sure that I understand your question, as you flagged this question as C and your code example is C++, but either way, if you're attempting to extend ruby you'll need to convert ruby values to C types, and if you plan on using C++ data structures, from C types to C++ objects.

Using the 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);
}

If you want to learn about passing structs back and forth between Ruby/C/C++, you'll need to have a thorough understanding of Ruby and the Ruby C API before you start trying to work with C++ objects.

Here are a few excellent resources to get you started:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.