Skip to content

Instantly share code, notes, and snippets.

View igrigorik's full-sized avatar
:octocat:

Ilya Grigorik igrigorik

:octocat:
View GitHub Profile
module EventMachine
# A simple iterator for concurrent asynchronous work.
#
# Unlike ruby's built-in iterators, the end of the current iteration cycle is signaled manually,
# instead of happening automatically after the yielded block finishes executing. For example:
#
# (0..10).each{ |num| }
#
# becomes:
#
require 'rubygems'
require 'eventmachine'
require 'em-http' # gem install em-http-request
require 'yajl' # gem install yajl-ruby
module ChatClient
def post_init
@name = "anonymous_#{ChatClient.client_num+=1}"
@sid = ChatClient.channel.subscribe(method(:send_msg))
@buf = ''
$_g := neo4j:open('neodb.neo')
$bob := g:add-v(g:map('name','Bob'))
$john := g:add-v(g:map('name','John'))
$alice := g:add-v(g:map('name','Alice'))
$e1 := g:add-e($bob,'link',$john)
$e2 := g:add-e($john,'link',$alice)
$e3 := g:add-e($alice,'link',$bob)
$e1/weight := 10
$e2/weight := 1
$e3/weight := 20
#!/bin/bash
# domainavailable
# Fast, domain name checker to use from the shell
# Use globs for real fun:
# domainavailable blah{1..3}.{com,net,org,co.uk}
# Inspired by foca / giles:
# http://gilesbowkett.blogspot.com/2009/02/simple-bash-domain-availability.html
trap 'exit 1' INT TERM EXIT
@igrigorik
igrigorik / stats.rb
Created March 25, 2010 04:37 — forked from tmm1/stats.rb
require 'socket'
require 'rubygems'
require 'collectd/pkt'
class Stats
def initialize name, instance, opts = {}
@name, @instance = name, instance
@sock = UDPSocket.new
@sock.connect opts[:host] || '239.192.74.66',
# Author: Pieter Noordhuis
# Description: Simple demo to showcase Redis PubSub with EventMachine
#
# Requirements:
# - rubygems: eventmachine, thin, cramp, sinatra, yajl-ruby
# - a browser with WebSocket support
#
# Usage:
# ruby redis_pubsub_demo.rb
#
class FiberedMutex
#
# Creates a new FiberedMutex
#
def initialize
@waiting = []
@locked = false
end
#
This example shows how to setup an environment running Rails 3 beta under 1.9.2-head with a 'rails3' gem set.
∴ rvm update --head
# ((Open a new shell)) or do 'rvm reload'
# If you do not already have the ruby interpreter installed, install it:
∴ rvm install 1.9.2-head
# Switch to 1.9.2-head and gemset rails3, create if it doesn't exist.
∴ rvm --create use 1.9.2-head@rails3
# minimal async sinatra app
# requires ruby 1.9.x
require 'sinatra'
require 'em-http'
require 'em-synchrony'
require 'em-synchrony/em-http'
require 'rack/fiber_pool'
use Rack::FiberPool
require "date"
require "time"
# converts base10 integers into NewBase60
def num_to_sxg(num=nil)
sxg = ""
vocabulary = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz"
return 0 if num.nil? || num.zero?