Ruby code in Elixir project

~/$ mix new ruby_elixir
* creating README.md
* creating .gitignore
* .....
# mix.exsdefp deps do
[{:export, “~> 0.0.7”},
{:erlport, github: “hdima/erlport”, manager: :make}]
end
~/ruby_elixir$ mix deps.get
* Getting erlport (https://github.com/hdima/erlport.git)
.......
Using locally cached package
# lib/ruby_elixir/ruby_call.exdefmodule RubyElixir.RubyCall do
use Export.Ruby
def ruby_call do
{:ok, ruby} = Ruby.start(ruby_lib: Path.expand(“lib/ruby”))
end
end
# lib/ruby/ruby.rbdef hello_world
puts “Hello from Ruby”
end
# lib/ruby_elixir/ruby_call.ex
......
def ruby_call do
{:ok, ruby} = Ruby.start(ruby_lib: Path.expand(“lib/ruby”))
ruby
|> Ruby.call(“ruby”, “hello_world”, [])
end
~/ruby_elixir$ iex -S mix
iex(1)> RubyElixir.RubyCall.ruby_call
Hello from Ruby
:undefined
# lib/ruby/ruby.rb
......
def hello(name)
puts “Hello #{name}”
end
--------------------------------------------------------------------# lib/ruby_elixir/ruby_call.ex
......
def ruby_call(name) do
{:ok, ruby} = Ruby.start(ruby_lib: Path.expand(“lib/ruby”))
ruby
|> Ruby.call(“ruby”, “hello”, [name])
end
iex> RubyElixir.RubyCall.ruby_call(“Stephan”)
Hello Stephan
:undefined
# Gemfilesource ‘https://rubygems.org'
gem ‘prawn’
~/ruby_elixir$ bundle
Fetching gem metadata from https://rubygems.org/
Fetching version metadata from https://rubygems.org/
Resolving dependencies…
Using pdf-core 0.6.1
......
# lib/ruby/ruby.rb
require "prawn"
......
def generate_pdf
Prawn::Document.generate("prawn_elixir_pdf.pdf") do
text "PDF generated with Ruby, Elixir and Prawn"
end
end
# lib/ruby_elixir/ruby_call.ex
......
def ruby_call do
{:ok, ruby} = Ruby.start(ruby_lib: Path.expand(“lib/ruby”))
ruby
|> Ruby.call(“ruby”, “generate_pdf”, [])
end
~/ruby_elixir$ iex -S mix
iex> RubyElixir.RubyCall.ruby_call
:undefined

Stephan Bakkelund Valois

Written by

I’m a Norwegian Ruby developer and a motorcycle enthusiast, currently learning and writing about Elixir and the Phoenix framework.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade