Slide 1

Slide 1 text

EXTENDING RUBY with C

Slide 2

Slide 2 text

Why?

Slide 3

Slide 3 text

PERFORMANCE

Slide 4

Slide 4 text

PERFORMANCE

Slide 5

Slide 5 text

C BINDINGS

Slide 6

Slide 6 text

C BINDINGS FFI

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

array  <<  1

Slide 10

Slide 10 text

static  VALUE rb_ary_push_1(VALUE  ary,  VALUE  item) {        long  idx  =  RARRAY_LEN(ary);        if  (idx  >=  ARY_CAPA(ary))  {                ary_double_capa(ary,  idx);        }        RARRAY_PTR(ary)[idx]  =  item;        ARY_SET_LEN(ary,  idx  +  1);        return  ary; }

Slide 11

Slide 11 text

EXT/WORLD/WORLD.C

Slide 12

Slide 12 text

>  World.hello =>  "Hello,  World!"

Slide 13

Slide 13 text

require  "world" describe  World  do    describe  "#hello"  do        it  "greets  the  world"  do            World.hello.should  ==  "Hello,  World!"        end    end end

Slide 14

Slide 14 text

module  World    module_function    def  hello        "Hello,  World!"    end end

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

HELLO

Slide 17

Slide 17 text

return  rb_str_new("Hello,  World!",  13);

Slide 18

Slide 18 text

static  VALUE  world_hello(VALUE  obj)  {    return  rb_str_new("Hello,  World!",  13); }

Slide 19

Slide 19 text

WORLD

Slide 20

Slide 20 text

VALUE  world_mWorld  =  rb_define_module("World"); rb_define_module_function(world_mWorld,  "hello",        world_hello,  0);

Slide 21

Slide 21 text

#include   static  VALUE  world_hello(VALUE  obj)  {    return  rb_str_new("Hello,  World!",  13); } void  Init_world(void)  {    VALUE  world_mWorld  =  rb_define_module("World");    rb_define_module_function(world_mWorld,  "hello",            world_hello,  0); }

Slide 22

Slide 22 text

COMPILATION

Slide 23

Slide 23 text

require  "mkmf"

Slide 24

Slide 24 text

require  "mkmf" $CFLAGS  <<  "  -­‐Wall  -­‐Wextra" create_makefile("world")

Slide 25

Slide 25 text

$  ruby  ext/world/extconf.rb creating  Makefile

Slide 26

Slide 26 text

$  make compiling  ext/world/world.c linking  shared-­‐object  world.bundle

Slide 27

Slide 27 text

$  rspec  spec/world_spec.rb   . Finished  in  0.00399  seconds 1  example,  0  failures

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

$  rake  compile

Slide 30

Slide 30 text

require  'rake/extensiontask' require  'rspec/core/rake_task' Rake::ExtensionTask.new('world') RSpec::Core::RakeTask.new('test') task  :test        =>  :compile task  :default  =>  :test

Slide 31

Slide 31 text

it  "greets  someone  by  name,  if  given"  do    World.hello("Bob").should  ==  "Hello,  Bob!" end

Slide 32

Slide 32 text

“If argc is -1, the function will be called as: VALUE func(int argc, VALUE *argv, VALUE obj) where argc is the actual number of arguments, argv is the C array of the arguments, and obj is the receiver.”

Slide 33

Slide 33 text

def  hello(name  =  nil)    if  name        "Hello,  #{name}!"    else        "Hello,  World!"    end end

Slide 34

Slide 34 text

rb_define_module_function(world_mWorld,        "hello",  world_hello,  -­‐1);

Slide 35

Slide 35 text

static  VALUE  world_hello(int  argc,  VALUE  *argv,  VALUE  obj)  {    VALUE  name;    rb_scan_args(argc,  argv,  "01",  &name);        ... }

Slide 36

Slide 36 text

static  VALUE  world_hello(int  argc,  VALUE  *argv,  VALUE  obj)  {    VALUE  name;    rb_scan_args(argc,  argv,  "01",  &name);    if  (NIL_P(name))  {        return  rb_str_new("Hello,  World!",  13);    }  else  {        ...    } }

Slide 37

Slide 37 text

static  VALUE  world_hello(int  argc,  VALUE  *argv,  VALUE  obj)  {    VALUE  name,  greeting;    rb_scan_args(argc,  argv,  "01",  &name);    if  (NIL_P(name))  {        greeting  =  rb_str_new("Hello,  World!",  13);    }  else  {        greeting  =  rb_str_new("Hello,  ",  7);        rb_str_cat(greeting,  RSTRING_PTR(name),  RSTRING_LEN(name));        rb_str_cat(greeting,  "!",  1);    }    return  greeting; }

Slide 38

Slide 38 text

$  rake  clean  test rm  -­‐r  tmp/x86_64-­‐darwin11.3.0/world/1.9.3 mkdir  -­‐p  tmp/x86_64-­‐darwin11.3.0/world/1.9.3 cd  tmp/x86_64-­‐darwin11.3.0/world/1.9.3 /Users/mudge/.rbenv/versions/1.9.3-­‐p125/bin/ruby  -­‐ I.  ../../../../ext/world/extconf.rb creating  Makefile cd  -­‐ cd  tmp/x86_64-­‐darwin11.3.0/world/1.9.3 make compiling  ../../../../ext/world/world.c linking  shared-­‐object  world.bundle cd  -­‐ install  -­‐c  tmp/x86_64-­‐darwin11.3.0/world/1.9.3/world.bundle  lib/ world.bundle /Users/mudge/.rbenv/versions/1.9.3-­‐p125/bin/ruby  -­‐S  rspec  ./ spec/world_spec.rb .. Finished  in  0.00081  seconds 2  examples,  0  failures

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

QUESTIONS?