11
Модули на C для Ruby Протасевич Владимир Racoons Group

Модули на C для Ruby

  • Upload
    racoons

  • View
    102

  • Download
    2

Embed Size (px)

DESCRIPTION

Протасевич Владимир в данной презентации познакомит вас поближе с возможностями и способами применения модулей на С для Ruby

Citation preview

Модули на C для Ruby

Протасевич Владимир Racoons Group

• Скорость • Порты библиотек(yaml)

Зачем?

hello_world.c

#include ruby.h !void init_hello_world() { printf("Hello World!"); }

build_ext.rbrequire «mkmf» !create_makefile("hello_world")

Done!

2.0.0-p353 :001 > require_relative ‘hello_world' => "Hello World!"

C++(Rice)

require "mkmf" → require "mkmf-rice" rb_define_method → define_method rb_define_class → define_class

hello_world.cpp

#include "rice/Class.hpp" void hello() { std::cout << "Hello World!"; } extern "C" void Init_hello_world() { Class test_ = define_class("Test") .define_method("hello", &hello); }

from_ruby() и to_ruby()

• Для многих встроенных типов уже есть • Если нет: ! template<> Foo from_ruby<Foo>(Object x) {}

А если надо портировать класс?

class Racoon { private: std::string m_name; int m_age; public: Racoon(std::string name, int age) { m_name = name; m_age = age; } void greeting() { std::cout << "Hello! My name is " << m_name << ". My age is " << m_age << std::endl; } };

Без проблем!extern "C" void Init_test() { Class rb_cRacoon = define_class<Racoon>("Racoon") .define_constructor(Constructor<Racoon, std::string, int>()) .define_method("greeting", &Racoon::greeting); } !!Ruby: 2.0.0-p353 :001 > Racoon.new('Vasya', 2).greeting Hello! My name is Vasya. My age is 2 => nil

Вопросы?