Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming conventions for Ruby C extension developers

Tags:

c

ruby

I'm interested in following the correct naming conventions when writing an extension for ruby in C. Specifically I'm referring things such adding _p to function names of predicates and prefixing variables with m for module, c for class etc.

For example, if we want to define a predicate method like the following in C, we should use _p as a suffix in the function that defines the method.

class MyClass
  def awesome?
    true
  end
end

In C:

static VALUE my_extension_my_class_awesome_p(VALUE self) {
  return Qtrue;
}

void Init_my_extension(void) {
  VALUE cMyClass = rb_define_class("MyClass", rb_cObject);

  rb_define_method(cMyClass,
                   "awesome?",
                   my_extension_my_class_awesome_p,
                   0);
}

Looking through the core Ruby source code I see suffixes for _p (predicate) and _m, which I'm not able to infer a meaning from. I'm sure there are a number of other conventions.

There are additional naming conventions, such as when to use underscores and when to use camel casing. It would be easy to create a mess without a guideline to follow when writing an extension with a substantial amount of C code.

Is there a definitive list somewhere? I never seem to turn up useful results when googling for Ruby C extension topics. Any quick examples that show the pure Ruby syntax and the equivalent C function named correctly?

like image 460
d11wtq Avatar asked Sep 17 '12 14:09

d11wtq


1 Answers

Here are a couple more: http://geoffgarside.co.uk/2007/05/20/ruby-c-extensions-nested-modules-classes/

Geoff Garside has a couple dozens repos written in ruby/C. He's pretty credible IMO. https://github.com/geoffgarside

I will keep looking for more and edit this post when I do find more.

EDIT

It looks like it's hard to find someone who wants to talk about ruby extension naming conventions... Maybe you could try sending a tweet/email in M. Garside's direction. He looks pretty active on twitter.

like image 168
mbinette Avatar answered Nov 15 '22 22:11

mbinette