Slide 1

Slide 1 text

GOING NATIVE WITH FFI Juan C. Ruiz

Slide 2

Slide 2 text

❏ Former software engineer @ MagmaLabs ❏ Remote work advocate ❏ RubyMe Q2 mentor ❏ I ❤ Open source @JuanCrg90

Slide 3

Slide 3 text

@JuanCrg90 Heroes' Causeway Arch Leon Guanajuato Photo By @marianozka

Slide 4

Slide 4 text

@JuanCrg90

Slide 5

Slide 5 text

@JuanCrg90

Slide 6

Slide 6 text

What are the options that we have to manage native code on Ruby? @JuanCrg90

Slide 7

Slide 7 text

MKMF @JuanCrg90

Slide 8

Slide 8 text

@JuanCrg90 mkmf.rb is used by Ruby C extensions to generate a Makefile which will correctly compile and link the C extension to Ruby and a third-party library.

Slide 9

Slide 9 text

library.h library.c @JuanCrg90

Slide 10

Slide 10 text

my_ruby_ext.c @JuanCrg90

Slide 11

Slide 11 text

extconf.rb @JuanCrg90

Slide 12

Slide 12 text

@JuanCrg90

Slide 13

Slide 13 text

@JuanCrg90

Slide 14

Slide 14 text

Pros: - We can use C functions in our Ruby Code - We take advantage of the existing ruby.h library to create the extension Cons: - We need to add extra C code to create the bindings - This solution only works in MRI @JuanCrg90

Slide 15

Slide 15 text

FFI @JuanCrg90

Slide 16

Slide 16 text

A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written in another. @JuanCrg90

Slide 17

Slide 17 text

@JuanCrg90

Slide 18

Slide 18 text

Pros: - We don’t need to add additional C code - An FFI extension is multi-platform and multi-implementation Cons: - Complex macros are difficult to maintain - Same for callback functions @JuanCrg90

Slide 19

Slide 19 text

PGM IMAGES @JuanCrg90

Slide 20

Slide 20 text

@JuanCrg90

Slide 21

Slide 21 text

@JuanCrg90

Slide 22

Slide 22 text

@JuanCrg90

Slide 23

Slide 23 text

@JuanCrg90

Slide 24

Slide 24 text

@JuanCrg90

Slide 25

Slide 25 text

@JuanCrg90

Slide 26

Slide 26 text

@JuanCrg90

Slide 27

Slide 27 text

@JuanCrg90

Slide 28

Slide 28 text

@JuanCrg90

Slide 29

Slide 29 text

@JuanCrg90

Slide 30

Slide 30 text

@JuanCrg90

Slide 31

Slide 31 text

@JuanCrg90

Slide 32

Slide 32 text

Conclusion @JuanCrg90

Slide 33

Slide 33 text

- https://github.com/JuanCrg90/my_ruby_ext - https://github.com/JuanCrg90/pgm_ffi @JuanCrg90

Slide 34

Slide 34 text

THANKS @JuanCrg90