#!/usr/bin/ruby require 'rubygems' require 'erlectricity' ## Avoid weird-!@#$! problem with: ## /usr/local/lib/ruby/gems/1.8/gems/erlectricity-0.2.0/lib/erlectricity/decoder.rb:7:in `read_any_from': uninitialized constant Erlectricity::Decoder::StringIO (NameError) ## from /usr/local/lib/ruby/gems/1.8/gems/erlectricity-0.2.0/lib/erlectricity/port.rb:42:in `read_from_input' ## [...] require 'active_support' STDERR.write "Before top of loop\r\n" # This 'while' wrapper loop is not necessary if each f.when() clause # includes a final call to f.receive_loop. However, it seems useful # to include the wrapper in case you forget to call f.receive_loop. # # Do not use "while 1 do" for the wrapper! If you do, this Ruby side # will go into an infinite, CPU-eating loop when STDIN and STDOUT # are closed by the Erlang side (e.g. by "erlang:port_close(RubyPort)"). while not STDIN.eof? do STDERR.write "At top of loop\r\n" receive do |f| STDERR.write "Before when 1\r\n" f.when(:hello, Any) do |term| STDERR.write ["got :hello ", term.inspect, "\r\n"] f.receive_loop end STDERR.write "After when 1\r\n" ## This clause will never match: it's exactly the same pattern ## as the previous clause. f.when(:hello, Any) do |term| STDERR.write ["got :hello (#2) ", term.inspect, "\r\n"] f.receive_loop end STDERR.write "After when 1b\r\n" f.when(:sleep, Any) do |amount| STDERR.write "Going to sleep for #{amount} seconds ... " sleep amount STDERR.write "done\r\n" f.send! [:done_sleeping, amount] f.receive_loop end f.when(:sample) do f.send! [:sample1, [:this, :is, :a, :tuple]] l = Erlectricity::List.new() ## l[1..4] = [:this, :is, :a, :list] f.send! [:sample2, l] f.receive_loop end f.when(Any) do |term| STDERR.write ["catchall: ", term.inspect, "\r\n"] ## Note: we omit calling f.receive_loop here to see what happens ## whith the after calls. ##### f.receive_loop end STDERR.write "After when 2\r\n" end end # Erlang equivalent, fwiw. # # receive_loop() -> # receive # {hello, Term} -> # io:format("got hello ~p\n", [Term]), # receive_loop(); # %% Erlang compiler will warn you about this. # {hello, Term} -> # io:format("got hello ~p\n", [Term]), # receive_loop(); # {sleep, Amount} -> # io:format("Going to sleep for ~p seconds ... "), # timer:sleep(trunc(Amount * 1000)), # receive_loop(); # Term -> # io:format("catchall: ~p\n", [Term]), # receive_loop() # end.