Did you know that you can fire up ruby code inside an Elixir project?

I haven’t done any benchmarks, but I have a feeling it would be too slow for production, however, coding and trying out crazy stuff is fun, right?

Let’s jump into it by creating a new project:

and cd into our project folder.

We are going to use ExPort - a wrapper for Erlport, a library for Erlang which helps connect Erlang to Python and Ruby. Now, I’m a Rubyist, so this blog post will be written in Ruby and Elixir.

To install the Export package, we’ll need to add it to our dependencies list:

We’ll then run mix deps.get:

So, in our lib folder, we’ll create two new folders - one named ruby, and one named ruby_elixir. Within the ruby_elixir folder, we’ll create a file called ruby_call.ex, and write the following into it:

Cool. So first we’re using the use macro to import the ExPort’s Ruby library, then we’ll tell our ruby_call/0 function where our Ruby folder is located.

In our ruby folder, we’ll create another file called ruby.rb, and write the following into it:

Now, back to our ruby_call.ex file:

Ruby.call takes 4 parameters. The ruby folder we got from the tuple, the filename, the ruby method, and the parameters. Since our ruby method doesn’t have any parameters, we should be good to go. Let’s fire up iEX and try it out:

Sweet. It worked! Let’s try with some parameters:

So we gave both our ruby method and our elixir function a new parameter name. Let’s see what happens if we run it:

Pretty awesome. Say we have a killer Ruby gem that we want to include in our Elixir project. Is it possible to do more “sophisticated” things with this? Let’s give it a shot..

I really like Prawn, a very nice PDF gem in Ruby. Now, to use Ruby gems, we need a Gemfile in our root folder. We’ll create one, and write the following in it:

Now, in our terminal, we’ll run bundle to install our gem:

Sweet! Looks like the gem is installed. Let’s see if we’re able to generate a pdf file with it. We’ll write up a new method in our ruby file:

And we’ll change our elixir function accordingly:

Let’s try and run it:

That didn’t tell us much, however, it didn’t blow up, which is always a great thing. Now, if we now take a look in our root folder, we’ve got a new file! prawn_elixir_pdf.pdf. If we open it up, we can see the content of our freshly generated pdf — PDF generated with Ruby, Elixir and Prawn.

Until next time
Stephan Bakkelund Valois

I’m a Norwegian developer and a motorcycle enthusiast. I love building cool stuff, and playing with exciting technology.

I’m a Norwegian developer and a motorcycle enthusiast. I love building cool stuff, and playing with exciting technology.