Skip to content

Instantly share code, notes, and snippets.

View jbr's full-sized avatar
:octocat:
Focusing on OSS for April

Jacob Rothstein jbr

:octocat:
Focusing on OSS for April
View GitHub Profile
@jbr
jbr / dj_monit_runner.sh
Created July 7, 2009 06:13
Delayed::Job runner for monit
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
APP_NAME=your_app_name_here #<--fill this in
APP_DIR=/var/www/apps/
RAILS_ROOT=$APP_DIR/current
if [ "$3" ]; then
RUNNER="$3"
else
RUNNER=1
V2UgYXJlIGEgcHJvZml0YWJsZSwgZmFzdCBncm93aW5nLCA0LXllYXIgb2xkLCBzdGVhbHRoIGNvbXBhbnkgbG9va2luZyBmb3IgYW4gYWRkaXRpb24gdG8gb3VyIGRldmVsb3BtZW50IHRlYW0uIE91ciBwYXN0IHN1Y2Nlc3MgaGFzIHB1dCB1cyBpbiB0aGUgbmljZSBwb3NpdGlvbiBvZiBiZWluZyBhYmxlIHRvIHNldCBvdXIgb3duIGNvdXJzZS4gV2UndmUgaGFkIHJlYWwgZ2FpbnMgZnJvbSB1c2luZyBjcnVmdC1mcmVlIHRlY2ggdG8gZW5oYW5jZSBvdXIgc3VjY2Vzc2Z1bCwgcHJvZml0YWJsZSwgaGlnaC12YWx1ZS1hZGQgc2VydmljZSwgc28gdGhlcmUgaXMgYSB3aWRlIGxhdGl0dWRlIGluIHVzaW5nIHRoZSBiZXN0IHRvb2wgZm9yIHRoZSBqb2IuIFdlIHVzZSBQeXRob24sIFJ1YnksIENvdWNoREIsIGFqYXggcHVzaCAoYWthIENvbWV0LCBlLmcuIE9yYml0ZWQsIEp1Z2dlcm5hdW50LCBldGMuKSBhbmQgdGhlIGxpa2U7IHdlIGRldmVsb3Agb24gTWFjIG9yIExpbnV4OyBkZXBsb3kgb24gQ2VudG9zLiBNb3N0IG9mIG91ciBkZXZlbG9wbWVudCBpcyBmb2N1c2VkIG9uIGZlYXR1cmVzIGFuZCBidXNpbmVzcyBsb2dpYywgYW5kIGxpdHRsZSBlbXBoYXNpcyBvbiBzY2FsaW5nIChzY2FsaW5nIGlzIGEgInNvbHZlZCBwcm9ibGVtLCIgYXMgdGhleSBzYXkpLiBXZSBzdHJpdmUgZm9yIGEgcmFwaWQgcmVsZWFzZSB0ZW1wbyBhbmQgcGxhbiBhY2NvcmRpbmdseS0gd2UgcmVsZWFzZSBmZWF0dXJlcyBpbiB3ZWVrcyBub3QgbW9udGhzLgoKUm9sZTog
def extract_regex(string)
string.scan(/([a-z_]+):(?:\/|%r\{)((?:(?:\\\/)|[^\/])+)(?:\/|\})/).inject({}) do |hash, match|
hash.merge match.first.to_sym => Regexp.new(match.last)
end
end
test_string = 'this is a a:/regular/ expression b:/with a \/ in it/ and some c:%r{other stuff}'
expected = {:a => /regular/, :b => /with a \/ in it/, :c => /other stuff/}
actual = extract_regex(test_string)
Ruby Quickstart for Refugees:
--
# is a comment.
You don't need semicolons.
Ruby aims to be elegant and readable, so punctuation and boilerplate are
minimal.
--
module Enumerable
def proxy(method) Proxy.new method, self end
class Proxy
instance_methods.each { |m| undef_method m unless m =~ /^__/ }
def initialize(method, enumerable)
@method, @enumerable = method, enumerable
end
git :init
git :add => "."
git :commit => "-a -m 'Initial commit'"
run "rm public/index.html"
plugin 'freighthopper', :git => 'git://github.com/jbr/freighthopper.git', :submodule => true
plugin 'jrails', :git => 'git://github.com/aaronchi/jrails.git', :submodule => true
plugin 'resourceful_loader', :git => 'git://github.com/jbr/resourceful_loader.git', :submodule => true
plugin 'url_param', :git => 'git://github.com/jbr/url_param.git', :submodule => true
class Rack::ResponseTimeInjector
def initialize(app, options = {})
@app = app
@format = options[:format] || "%f"
end
def call(env)
t0 = Time.now
returning @app.call(env) do |response|
response.last.body.gsub! /\$responsetime(?:\((.+)\))?/ do
>> ObjectSpace.each_object{|o| p o if Class === o && !o.instance_methods.include?('dup')}
#=> 98690
>> class Fixnum; undef :dup; end
#=> nil
>> ObjectSpace.each_object{|o| p o if Class === o && !o.instance_methods.include?('dup')}
Fixnum
#=> 122815
@jbr
jbr / README.textile
Created October 27, 2009 21:42
How I switch between projects

Stick this in the bottom of ~/.profile & update PROJECT_BASE
Assuming you have a bunch of projects in PROJECT_BASE, you can do

bash> project my_project_name

You’ll now be in $PROJECT_BASE/my_project_name
Any new bash sessions you start will begin in that directory, which makes opening new tabs really convenient.
You can also just do

bash> project
require 'onion'
Onion.new Stats, Memoizer, TimesOut do
config :stats => true, :timeout => 1_000, :pool_size => 20
puts "Forward:"
transaction do
query "SELECT ... FROM ... FOR UPDATE ..."
execute "INSERT ..."