<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Adrian E.</title>
    <description>The latest articles on Forem by Adrian E. (@_adrian_e_).</description>
    <link>https://forem.com/_adrian_e_</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F158085%2F7fd5704b-e13f-45f4-ae78-566aaba2ecc2.png</url>
      <title>Forem: Adrian E.</title>
      <link>https://forem.com/_adrian_e_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/_adrian_e_"/>
    <language>en</language>
    <item>
      <title>How to extend Ruby with C++</title>
      <dc:creator>Adrian E.</dc:creator>
      <pubDate>Sun, 16 Aug 2020 08:58:06 +0000</pubDate>
      <link>https://forem.com/_adrian_e_/extend-ruby-with-c-14mh</link>
      <guid>https://forem.com/_adrian_e_/extend-ruby-with-c-14mh</guid>
      <description>&lt;p&gt;To extend Ruby with C++ is not that hard as you might think. Within a &lt;a href="https://github.com/addisaden/rbextample"&gt;small project&lt;/a&gt; I created a barebone structure to getting started.&lt;/p&gt;

&lt;p&gt;Here you will find answers for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What libs do I need?&lt;/li&gt;
&lt;li&gt;How can I compile?&lt;/li&gt;
&lt;li&gt;And how can I interact with my C++-Class?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's start the adventure 🤠&lt;/p&gt;

&lt;h1&gt;
  
  
  The code
&lt;/h1&gt;

&lt;p&gt;First we need something to work with. Well a featureless &lt;a href="https://github.com/addisaden/rbextample/blob/master/Car.hpp"&gt;Car class&lt;/a&gt; work for that. The goal is to create and control the car. Well it's really featureless, because you can only give gas or stop it. But hey the car talks when it's born.&lt;/p&gt;

&lt;h1&gt;
  
  
  The libs
&lt;/h1&gt;

&lt;p&gt;The default is &lt;a href="https://ruby-doc.org/stdlib-2.7.1/libdoc/mkmf/rdoc/MakeMakefile.html"&gt;mkmf&lt;/a&gt; (epsecially when you use the C-Api).&lt;/p&gt;

&lt;p&gt;For C++ there is the great gem &lt;a href="https://rubygems.org/gems/rice"&gt;rice&lt;/a&gt; which is a great tool to bind a c++ class to ruby.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/addisaden/rbextample/blob/master/car_wrapper.cpp"&gt;car_wrapper.cpp&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// includes goes here
extern "C"
void Init_car_wrapper()
{
    Data_Type&amp;lt;Car&amp;gt; rb_cCar =
        define_class&amp;lt;Car&amp;gt;("Car")
            .define_constructor(Constructor&amp;lt;Car&amp;gt;())
            .define_method("gas", &amp;amp;Car::gas)
            .define_method("stop", &amp;amp;Car::stop);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So you wrap the class and every method into rice methods. To dig deeper you can read the &lt;a href="https://jasonroelofs.com/rice/"&gt;documentation&lt;/a&gt; 🙂&lt;/p&gt;

&lt;h1&gt;
  
  
  compiling
&lt;/h1&gt;

&lt;p&gt;This is really beautiful. Just create a &lt;a href="https://github.com/addisaden/rbextample/blob/master/extconf.rb"&gt;extconf.rb&lt;/a&gt; file with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require "mkmf-rice"
create_makefile "car_wrapper"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;ruby extconf.rb&lt;/code&gt; and you get a &lt;code&gt;Makefile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally run &lt;code&gt;make&lt;/code&gt; and you get your Ruby-Extension as &lt;code&gt;car_wrapper.so&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  playground
&lt;/h1&gt;

&lt;p&gt;And here you can play with it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1] pry(main)&amp;gt; require "./car_wrapper"
=&amp;gt; true
[2] pry(main)&amp;gt; mycar = Car.new
A new Car is born
=&amp;gt; #&amp;lt;Car:0x00005557f9db5bb8&amp;gt;
[3] pry(main)&amp;gt; mycar.gas(50)
=&amp;gt; 50
[4] pry(main)&amp;gt; mycar.stop()
=&amp;gt; 20
[5] pry(main)&amp;gt; mycar.stop()
=&amp;gt; -10
[6] pry(main)&amp;gt; mycar.stop()
=&amp;gt; -40
[7] pry(main)&amp;gt; exit
Good bye my car...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;🥳 have fun 🎉&lt;/p&gt;

&lt;h1&gt;
  
  
  Photo credits
&lt;/h1&gt;

&lt;p&gt;car: Thanks to &lt;code&gt;MovienerdDeluxe&lt;/code&gt; - &lt;a href="https://de.wikipedia.org/wiki/Datei:K.I.T.T._Nachbau_aus_Deutschland.jpg"&gt;source&lt;/a&gt;&lt;br&gt;
ruby-logo: Thanks to &lt;code&gt;Yukihiro Matsumoto, Ruby Visual Identity Team&lt;/code&gt; - &lt;a href="https://www.ruby-lang.org/en/about/logo/"&gt;source&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rubygems</category>
      <category>cpp</category>
      <category>howto</category>
    </item>
  </channel>
</rss>
