-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rb
55 lines (46 loc) · 1.28 KB
/
test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/ruby
require 'rubygems'
require 'rbridge'
require 'date'
sync = RBridge.new('lists', 'localhost', 9000, false)
# synchronous method calls
p sync.sort(["Take Five", "All of Me", "'round Midnight", "Cousin Mary"])
p sync.erl(<<-ERL
F = fun(X) -> string:to_upper(X) end,
lists:map(F, ["Naima", "So What", "Bluebird"]).
ERL
)
["string", "10", "1.234", "atom", "{this,is,a,tuple}",
"[this,is,a,list]", "<<\"binary\",10,13>>", "<<1,2,3,4,5>>"
].each do |expr|
val = sync.erl(expr + ".")
puts "#{val.inspect} -> #{val.class}"
end
# async process calls
async = RBridge.new(nil, 'localhost', 9000, true)
puts "Before call: #{DateTime.now}"
thr = async.erl(%q[
{ok, Id} = http:request(get, {"http://www.allmusic.com", []}, [], [{sync,false}]),
receive {http, {Id, {Status, Headers, Body}}} ->
Status
end.
],
Proc.new {|val| puts "Block called at #{DateTime.now} with value:\n #{val.inspect}"}
)
puts "After call: #{DateTime.now}"
thr.join
# monkey patch for :erl method
class RBridge
alias old_erl erl
def erl(command, &block)
if block_given?
old_erl(command, Proc.new(&block))
else
old_erl(command)
end
end
end
thr = async.erl("1+2.") do |val|
puts "Block called with value #{val}"
end
thr.join