Using ruby's BigDecimal in the C API
-
28-05-2021 - |
Question
In order to create a BigDecimal from a C string in a Ruby extension, I'm doing this:
rb_funcall(rb_path2class("BigDecimal"), rb_intern("new"), 1, rb_str_new("0.0777", 6));
// => BigDecimal.new("0.0777")
Is there a shorter way to do this?
Also, stupid question, but is there an 'official' documentation for the C API (ruby 1.9.3), or does it just come down to reading the headers?
Solution
Unfortunately, the initialize
function, and pretty much the entire BigDecimal
C API, is declared as static and is thus not exposed.
The best way to learn about the C implementation of Ruby, and its API, is to browse the source, especially the ext
directory. There is also the README.EXT file, which describes the general API.
OTHER TIPS
I do not get what the problem really is. You do like it shorter? Write a wrapper.
rb_object new_big_decimal(char * from) {
rb_funcall(rb_path2class("BigDecimal"), rb_intern("new"), 1, rb_str_new(from, 6));
}
Of course it may not be rb_object but something else, but what is the problem?