Erlectricity exposes Ruby to Erlang and vice versa.
Ruby C Erlang Shell
Failed to load latest commit information.
benchmarks Erlectricity::Decoder.read_any_from -> Erlectricity::Decoder.decode Apr 28, 2009
examples support boolean encoding and matching Apr 28, 2009
ext optimize strings in c decoder Oct 28, 2009
lib fix bignum encoding Oct 28, 2009
test prevent stack overflow for massive binaries in c decoder Oct 28, 2009
.gitignore fix null byte causing premature termination of strings on decode May 4, 2009
History.txt update history for 1.1.1 release Oct 28, 2009
LICENSE better production/steady terminology Apr 29, 2009
Rakefile Erlectricity::Decoder.read_any_from -> Erlectricity::Decoder.decode Apr 28, 2009
VERSION.yml Version bump to 1.1.1 Oct 28, 2009
erlectricity.gemspec Regenerated gemspec for version 1.1.1 Oct 28, 2009


By Scott Fleckenstein, Tom Preston-Werner

Development Status: Production/Stable


Erlectricity allows a Ruby program to receive and respond to Erlang messages sent over the Erlang binary protocol.


$ gem install erlectricity


$ gem install mojombo-erlectricity -s

The Simplest Example

Ruby side (echo.rb)

require 'rubygems'
require 'erlectricity'

receive do |f|
  f.when([:echo, String]) do |text|
    f.send!([:result, "You said: #{text}"])

Erlang side (echo.erl)


test() ->
  Cmd = "ruby echo.rb",
  Port = open_port({spawn, Cmd}, [{packet, 4}, nouse_stdio, exit_status, binary]),
  Payload = term_to_binary({echo, <<"hello world!">>}),
  port_command(Port, Payload),
    {Port, {data, Data}} ->
      {result, Text} = binary_to_term(Data),
      io:format("~p~n", [Text])

Data Type Conversions and Matching

% Port is the port opened via open_port({spawn, Cmd}, [{packet, 4}, ...])
% Message is the Erlang term to encode and send to the port
send(Port, Message) ->
  port_command(Port, term_to_binary(Message)).

# Each triplet below represents:
# (line 1) the Erlang call
# (line 2) the Ruby matcher
# (line 3) the Ruby output

send(Port, test).
f.when(:test) { p :ok }
# :ok

send(Port, {atom, symbol}).
f.when([:atom, Symbol]) { |sym| p sym }
# :symbol

send(Port, {number, 1}).
f.when([:number, Fixnum]) { |num| p num }
# 1

send(Port, {string, <<"foo">>}).
f.when([:string, String]) { |str| p str }
# "foo"

send(Port, {array, [1,2,3]}).
f.when([:array, Array]) { |arr| p arr }
# [1, 2, 3]

send(Port, {array, [<<"abc">>, <<"def">>]}).
f.when([:array, Array]) { |arr| p arr }
# ["abc", "def"]

send(Port, {hash, [{key,val}]}).
f.when([:hash, Erl.hash]) { |hash| p hash }
# {:key=>:val}

send(Port, {object, {1,{2},3,<<"four">>}}).
f.when([:object, Any]) { |any| p any }
# [1, [2], 3, "four"]


If you'd like to hack on Erlectricity, start by forking my repo on GitHub:

To get all of the dependencies, install the gem first. The best way to get your changes merged back into core is as follows:

  1. Clone down your fork
  2. Create a topic branch to contain your change
  3. Hack away
  4. Add tests and make sure everything still passes by running rake
  5. If you are adding new functionality, document it in the
  6. Do not change the version number, I will do that on my end
  7. If necessary, rebase your commits into logical chunks, without errors
  8. Push the branch up to GitHub
  9. Send me (mojombo) a pull request for your branch


Copyright (c) 2009 Scott Fleckenstein and Tom Preston-Werner. See LICENSE for details.