Skip to content

Commit

Permalink
Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gsterndale committed Dec 21, 2011
1 parent 75bee3e commit 85f92a7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
28 changes: 28 additions & 0 deletions examples/io.rb
@@ -0,0 +1,28 @@
#!/usr/bin/env ruby
# encoding: UTF-8

$LOAD_PATH.unshift(File.dirname(__FILE__)+'/../lib/')
require 'reactorb'

message = ['0'..'9', 'A'..'Z', 'a'..'z'].map(&:to_a).flatten.join

reader, writer = IO.pipe

Reactor.new.run do |reactor|

reactor.attach writer, :write do |write_io|
chunk = message.slice!(0..9)
write_io.write chunk
puts "Wrote:\t#{chunk}"
write_io.close if message.empty?
end

reactor.attach reader, :read do |read_io|
if read_io.eof?
read_io.close
else
puts "Read:\t#{read_io.read 5}"
end
end

end
29 changes: 29 additions & 0 deletions examples/timers.rb
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby
# encoding: UTF-8

$LOAD_PATH.unshift(File.dirname(__FILE__)+'/../lib/')
require 'reactorb'

n = 0

Reactor.new.run do |reactor|
reactor.at(Time.now + 1) do
puts "one"
n += 1
end

reactor.in(2) do
puts "two"

reactor.in(1, n + 2) do |sum|
puts "three"

reactor.at(Time.now + 4, sum + 3) do |sum|
puts "four"
n = sum + 4
puts n
end
end
end
end

0 comments on commit 85f92a7

Please sign in to comment.