Redis is my favourite key/value store; it’s flexible, easy to set up and insanely fast. [EventMachine][] is a popular Ruby library for doing asynchronous I/O using an event loop. Bindings already [exist][em-redis] for accessing a Redis server using EM’s async-I/O (courtesy of Jonathan Broad), but unfortunately the resulting code has to use [Continuation-Passing Style][cps] via Ruby blocks. A very basic example of what that looks like follows:
This file contains hidden or 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
| if [ "$1" ]; then | |
| BRANCH=$1 | |
| else | |
| BRANCH=$(git branch | grep '*' | cut -d ' ' -f2) | |
| fi | |
| REMOTE=$(git config --get branch.$BRANCH.remote) | |
| REMOTE_BRANCH=$(git config --get branch.$BRANCH.merge) | |
| echo "$REMOTE/${REMOTE_BRANCH##*/}" |
This file contains hidden or 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
| framework 'Cocoa' | |
| # Documentation for delegates: | |
| # http://developer.apple.com/mac/library/documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html | |
| class RSSParser | |
| attr_accessor :parser, :xml_url, :doc | |
| def initialize(xml_url) | |
| @xml_url = xml_url |
This file contains hidden or 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
| framework 'Cocoa' | |
| # Documentation for delegates: | |
| # http://developer.apple.com/mac/library/documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html | |
| class RSSParser | |
| attr_accessor :parser, :xml_url, :doc | |
| def initialize(xml_url) | |
| @xml_url = xml_url |
This file contains hidden or 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 'open-uri' | |
| # url dsl -- the ultimate url dsl! | |
| # | |
| # You just can't beat this: | |
| # | |
| # $ irb -r url_dsl | |
| # >> include URLDSL | |
| # => Object | |
| # >> http://github.com/defunkt.json |
This file contains hidden or 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 Class | |
| def memoize(*methods) | |
| methods.each do |method_name| | |
| safe_method_name = method_name.to_s.gsub(/(\!|\?)/, '_') | |
| class_eval(" | |
| alias :'#{safe_method_name}_without_memo' :'#{method_name}' | |
| def #{method_name} | |
| if defined?(@#{safe_method_name}) | |
| @#{safe_method_name} |
This file contains hidden or 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
| #! /bin/sh | |
| ### BEGIN INIT INFO | |
| # Provides: redis-server | |
| # Required-Start: $syslog | |
| # Required-Stop: $syslog | |
| # Should-Start: $local_fs | |
| # Should-Stop: $local_fs | |
| # Default-Start: 2 3 4 5 | |
| # Default-Stop: 0 1 6 | |
| # Short-Description: redis-server - Persistent key-value db |
This file contains hidden or 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 'benchmark' | |
| def each_acc list | |
| foos = [] | |
| list.each do |letter| | |
| foos << letter.to_i | |
| end | |
| foos | |
| end |
This file contains hidden or 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
| namespace :harmony do | |
| desc "Munges the data" | |
| task :munge => :environment do | |
| docs_with_publish = Item.collection.find({'publish' => {'$exists' => true}}).to_a | |
| puts "Item count: #{Item.count}" | |
| puts "Items with publish key: #{docs_with_publish.size}" | |
| docs_with_publish.each do |hash| | |
| hash.delete('publish') |
This file contains hidden or 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 'pp' | |
| require 'rubygems' | |
| require 'mongo_mapper' | |
| MongoMapper.database = 'testing' | |
| class Rating | |
| include MongoMapper::EmbeddedDocument | |
| key :user_id, ObjectId |