Skip to content

Instantly share code, notes, and snippets.

@gregorymostizky
gregorymostizky / sinatra_fiber.rb
Created December 28, 2010 18:27
Patches async sinatra to use Fiber.new{}.resume for every request
#encoding: utf-8
# Patches async sinatra to use Fiber.new{}.resume for every request
require "sinatra/async"
require "fiber"
module Sinatra
module Async
module Helpers
@gregorymostizky
gregorymostizky / redis.expect
Created January 25, 2011 13:22
Check if redis is up using telnet and expect
set timeout 5
spawn telnet [lindex $argv 0] [lindex $argv 1]
expect "Connected"
send "PING\n"
expect "+PONG"
send "QUIT"
#usage: expect redis.expect <host> <port>
@gregorymostizky
gregorymostizky / redis-em-patch
Created January 25, 2011 16:16
Patch redis and em-redis to work together
require 'em-redis'
require 'redis'
require 'redis/distributed'
require "fiber_pool"
class Redis
class Distributed
def initialize(urls, options = {})
@tag = options.delete(:tag) || /^\{(.+?)\}/
@gregorymostizky
gregorymostizky / svn ignore recursive
Created January 30, 2011 10:44
Recursive SVN ignore from command line
echo -e ".backup\nlogs\nvendor" | xargs -d# -Ixx svn propset svn:ignore 'xx' . --recursive
@gregorymostizky
gregorymostizky / mysql2_connection_pool.rb
Created February 1, 2011 15:06
Simple connection pool for evented mysql2
require 'mysql2'
require 'mysql2/em'
class MysqlConnectionPool
def initialize(conf)
@pool_size = conf[:size] || 10
@connections = []
@query_queue = EM::Queue.new
start_queue conf
@gregorymostizky
gregorymostizky / commas.rb
Created February 10, 2011 16:35
Insert commas into a number string
str.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,'\1,').reverse
@gregorymostizky
gregorymostizky / hpricot.rb
Created March 2, 2011 13:39
Simple web scraping in few lines of code
require 'hpricot'
require 'open-uri'
doc = open('http://www.articlesbase.com/investing-articles/tips-for-successful-private-placement-trading-4339216.html') {|f| Hpricot(f)}
doc.search("script").remove
doc.search("style").remove
puts doc.search("body").text
@gregorymostizky
gregorymostizky / jquery-custom-fn.js
Created April 13, 2011 13:42
Adding custom function to JQuery
// definition
$.fn.customFunction = function(options) {
var defaults = {
width: 650,
height: 450
};
var opts = $.extend(defaults, options);
var self = $(this[0]); // if working on first/single element
// functionality here
@gregorymostizky
gregorymostizky / jquery-custom-event.js
Created April 13, 2011 13:44
Bind and trigger custom events for specific element
var self = $('#mydiv');
// define
self.unbind('my-event');
self.bind('my-event', function() {alert('yes')});
// trigger
self.trigger('my-event');
@gregorymostizky
gregorymostizky / array_nil_compare_hack.rb
Created April 28, 2011 13:45
Allows comparing arrays with nils
#usage
a = [1,2]
b = [1,nil]
a.extend(ArrayWithNilCompareHack)
b.extend(ArrayWithNilCompareHack)
a<=>b # works
# code
module ArrayWithNilCompareHack
def <=>(other)