Skip to content

Instantly share code, notes, and snippets.

require 'rubygems'
require 'spec'
include Spec::Matchers
a = (1..10).to_a
b = (5..15).to_a
matcher = MatchArray.new(a)
matcher.matches?(b) # => false
require 'rubygems'
require 'eventmachine'
require 'dataflow'
# Experimenting with dataflow + EM
def do_pop_stuff
pop_client = EventMachine::POP::POP3Client.new('pop.gmail.com', 995, true, 'username', 'password')
print "stat: #{pop_client.stat}\n"
pop_client.mails.each do |m|
print m.raw + "\n"
require 'rubygems'
require 'eventmachine'
require 'em-http'
require 'dataflow'
Thread.new {
EM.run{}
}
module EventMachine
require 'rubygems'
require 'mysqlplus'
m = Mysql.real_connect('localhost','root')
m.send_query("SELECT 1")
p m.stat # should raise error?
m.get_result() # hangs
cursor = 0
a = (1..5).to_a
b = []
stack = []
while true
if cursor < a.size
b << a[cursor]
stack << cursor
# mixin to measure up/down rate for an EM::Connection
module RateMeasure
RATE_PERIOD = 5
def post_init
setup_meters
super
end
def receive_data data
update_meter(data.length, @download_meter)
exports.SHA1 = function() {
this.H0 = 0x67452301;
this.H1 = 0xEFCDAB89;
this.H2 = 0x98BADCFE;
this.H3 = 0x10325476;
this.H4 = 0xC3D2E1F0;
this.buffer = "";
this.message_length = 0;
}
# Implementation of Timeout with EventMachine timers
require 'timeout'
module EMTimeout
# TODO make sure EM is running and make sure this isn't called by the reactor thread
def timeout(sec, klass = nil)
return yield if sec == nil or sec.zero?
raise ThreadError, "timeout within critical session" if Thread.critical
exception = klass || Class.new(ExitException)
x = Thread.current
class ImapConnection < EM::Connection
include EM::Protocols::LineText2
def post_init
@state = :unconnected
@tag_count = 0
@need_bytes = 0
end
def connection_completed
avg-cpu: %user %nice %system %iowait %steal %idle
0.00 0.00 0.00 25.00 0.00 75.00
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await svctm %util
sda1 0.00 0.07 0.00 0.20 0.00 1.07 10.67 0.00 0.00 0.00 0.00
sdb 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
sde 0.00 0.03 0.00 2.83 0.00 14.57 10.28 110.83 41245.49 353.00 100.02