This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'eventmachine' | |
# Allows calling set/clear at will, and ensures that only a single on_set or | |
# on_clear deferrable is running concurrently. It also ensures that | |
# un-necessary callbacks are not run, i.e. set-unset-set in sequency only | |
# requires the on_set callback to run once | |
# | |
# The blocks/procs supplied for on_set and on_clear must return deferrables | |
# | |
class Thing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'em-hiredis' | |
class EM::Hiredis::Client | |
class Transaction | |
include EM::Deferrable | |
def initialize(redis) | |
@redis = redis | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'eventmachine' | |
require 'em-hiredis' | |
require 'fiber' | |
class Cache | |
include EM::Deferrable | |
def initialize(key) | |
$redis.get(key) { |v| | |
@value = v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source :rubygems | |
gem 'em-websocket' | |
gem 'sinatra' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'em-hiredis' | |
EM.run { | |
require 'em-hiredis' | |
redis = EM::Hiredis.connect | |
# If you pass a block to subscribe it will be called whenever a message | |
# is received on this channel | |
redis.pubsub.subscribe('foo') { |msg| | |
p [:received_foo, msg] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'em-hiredis' | |
EM.run { | |
redis = EM::Hiredis.connect | |
redis.set('foo', 'value') | |
redis.watch('foo') { | |
puts "Watching - try changing key foo within 1s" | |
EM.add_timer(1) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'eventmachine' | |
require 'em-websocket' | |
class CustomWSConnection < EventMachine::WebSocket::Connection | |
def trigger_on_message(msg) | |
send "Pong: #{msg}" | |
end | |
def trigger_on_open | |
send "Hello Client!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'net/http' | |
require 'uri' | |
require 'json' | |
class GoogleClosure | |
class Error < RuntimeError; end | |
HOST = URI.parse('http://closure-compiler.appspot.com/compile') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Easily calculate mean and standard distribution of a distribution without | |
# collecting all values in memory | |
# | |
class Distribution | |
def initialize | |
@n, @sum_x, @sum_x_2 = 0, 0, 0 | |
end | |
def <<(x) | |
@n += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Command | |
class << self | |
def run(command) | |
output = `#{command} 2>&1`.chomp | |
if $? != 0 | |
raise " *** Command `#{command}` failed with the following output:\n#{output}" | |
end | |
output | |
end | |
end |