Skip to content

Instantly share code, notes, and snippets.

@ybur-yug
Created September 10, 2017 19:09
Show Gist options
  • Save ybur-yug/05486f22c13d13b53d262d5640fb7f05 to your computer and use it in GitHub Desktop.
Save ybur-yug/05486f22c13d13b53d262d5640fb7f05 to your computer and use it in GitHub Desktop.
cmd = ~S"""
ruby -e '
require "bundler"
require "erlang/etf"
require "stringio"
require "sinatra"
@input = IO.new(3)
@output = IO.new(4)
@output.sync = true
def receive_input
encoded_length = @input.read(4)
return nil unless encoded_length
length = encoded_length.unpack("N").first
Erlang.binary_to_term(@input.read(length))
end
def send_response(value)
response = Erlang.term_to_binary(Erlang::Tuple[:response, value])
@output.write([response.bytesize].pack("N"))
@output.write(response)
true
end
context = binding
get "/" do
"hello"
end
while (cmd = receive_input) do
if cmd.is_a?(Erlang::Tuple) && cmd[0] == :eval
puts "Ruby: #{cmd[1]}"
res = eval(cmd[1], context)
puts "Ruby: => #{res.inspect}\n\n"
end
end
'
"""
send_eval = fn(port, command) ->
Port.command(port, :erlang.term_to_binary({:eval, command}))
end
port = Port.open({:spawn, cmd}, [:binary, {:packet, 4}, :nouse_stdio])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment