4

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?

Matheus Moreira
  • 17,106
  • 3
  • 68
  • 107
d11wtq
  • 34,788
  • 19
  • 120
  • 195

2 Answers2

2

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?

Friedrich
  • 5,916
  • 25
  • 45
  • No problem, I just thought all the numeric types may have had macros, like how in ruby you don't need `BigDecimal.new(..)`, as `BigDecimal(..)` will suffice. Was just wondering if I was doing it wrong :) – d11wtq Mar 31 '12 at 10:03
1

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.

Matheus Moreira
  • 17,106
  • 3
  • 68
  • 107