Project

extpp

0.04
The project is in a healthy, maintained state
You can write your Ruby extension easier than Ruby's C API. Because Ext++'s C++ API reduces duplicated code needed for Ruby's C API.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
 Dependencies

Development

 Project Readme

README

Name

Ext++

Description

Ext++ is a Ruby extension that provides C++ API for writing Ruby extension.

You can write your Ruby extension easier than Ruby's C API. Because Ext++'s C++ API reduces duplicated code needed for Ruby's C API.

You can use all Ruby's C API without any adapter layer. Because you can use C API directory from C++.

Install

% gem install extpp

How to use

Here is an example to create Hello class.

Create hello.cpp:

#include <ruby.hpp>

RB_BEGIN_DECLS

void
Init_hello(void)
{
  rb::Class klass("Hello");
  klass.define_method("initialize", [](VALUE rb_self, int argc, VALUE *argv) {
      VALUE rb_name;
      rb_scan_args(argc, argv, "1", &rb_name);
      rb::Object self(rb_self);
      self.ivar_set("@name", rb_name);
      return Qnil;
    });
  klass.define_method("greet", [](VALUE rb_self) {
      rb::Object self(rb_self);
      rb::Object prefix(rb_str_new_cstr("Hello "));
      auto message = prefix.send("+", {self.ivar_get("@name")});
      return static_cast<VALUE>(message);
    });
}

RB_END_DECLS

This code equals to the following Ruby code:

class Hello
  def initialize(name)
    @name = name
  end

  def greet
    "Hello " + @name
  end
end

Create extconf.rb:

require "extpp"

create_makefile("hello")

Create Makefile:

% ruby extconf.rb

Build this extension:

% make

Now, you can use this extension:

% ruby -r ./hello -e 'p Hello.new("me").greet'
"Hello me"

License

Copyright (C) 2017-2021 Sutou Kouhei

The 2-Clause BSD License. See LICENSE.txt for details.