Skip to content

Instantly share code, notes, and snippets.

require 'test/unit'
require 'shoulda'
class Test::Unit::TestCase
def self.context_helper(helper_arg, &block)
context "context helper #{helper_arg}" do
setup do
@helper_arg = helper_arg
end
merge_block(&block) if block_given?
# http://tomayko.com/writings/unicorn-is-unix
# simple preforking echo server in Ruby
require 'socket'
# Create a socket, bind it to localhost:4242, and start listening.
# Runs once in the parent; all forked children inherit the socket's
# file descriptor.
acceptor = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
address = Socket.pack_sockaddr_in(4242, 'localhost')
acceptor.bind(address)
def inner(&block)
@inner = block if block_given?
@inner
end
outter = lambda do
name = 'outter'
inner do
puts "inner sees #{name}"
end
@gotascii
gotascii / pygrack.rb
Created November 6, 2009 21:23
Pygments syntax highlighting via the unofficial pygments API http://pygments.appspot.com/
require 'net/http'
require 'uri'
require 'nokogiri'
class Pygrack
def initialize(app)
@app = app
end
def call(env)
.bundle/environment.rb
bundler_gems
@gotascii
gotascii / tco.rb
Created October 25, 2010 22:46
continuations via blocks and exceptions
# I got this idea from http://mihai.bazon.net/blog/redis-client-library-javascript-node
# The technique relies on the fact that raising an exception clears the call stack.
# The context is passed along with a block attached to an exception.
# I thought it was brilliant js hackery so I decided to try my hand at it in ruby.
# I've also included some other stack-dependent implementations.
# straight recursion, not quite a tail-call
# I can't go above 8.1k without stack error
def rsum(num)
if num == 0
Object.prototype.inspect = function () {
function pad(depth) {
return Array(depth).join(" ");
}
function down(attr, val, depth) {
var output = pad(depth) + attr + ': ';
output += (typeof(val) == 'object' ? "{" : val);
return output + "\n";
}
class String
def named_matches(rx)
m = rx.match(self)
[Hash[m.names.collect{|n| [n, m[n]]}]]
end
end
def PostsController < ActionController::Base
def create
@post = Post.new(params[:post])
if @post.save
redirect_to posts_path
else
render :action => "new"
end
end
@gotascii
gotascii / simple_deployer.rb
Created March 22, 2011 14:46
cc.rb auto-deployer
# This plugin will deploy your app using capistrano.
# If the build fails the app won't de be deployed and if it passes it will try to deploy to a list of stages you can supply.
#
# Configuration
# In your project's cruise_config.rb file:
#
# Pass an array of stages you want to deploy to
# project.cap_deployer.stages = ['integration', 'staging']
#
# source ~/.rvm/scripts/rvm before deployment?