Skip to content

Instantly share code, notes, and snippets.

View ruby_subprocesses_part_2.rb
# :PROCESS: ruby, "ruby %f 2>&1"
# :BRACKET_CODE: "[ruby]", "[/ruby]"
# :TEXT:
# In the <a
# href="">previous
# article</a> we looked at some basic methods for starting subprocesses in Ruby.
# One thing all those methods had in common was that they didn't permit a lot of
# communication between parent process and child. In this article we'll examine
# a few built-in Ruby methods which give us the ability to have a two-way
View gist:211649
ruby -e 'puts Object.constants.grep( /RUBY/ ).map{ |c| "##{[c, Object.const_get(c)].inspect}"}'
View rbconfig
#!/usr/bin/env ruby
require 'rbconfig'
RUBY = Config::CONFIG['bindir'] + '/' + Config::CONFIG['RUBY_INSTALL_NAME'] + Config::CONFIG['EXEEXT']
BOLD = "\e[1m"
WHITE = "\e[37m"
RED = "\e[31m"
GREEN = "\e[32m"
YELLOW = "\e[33m"
BLACK_BG = "\e[40m"
BLUE_BG = "\e[44m"
View webapp.rb
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
View gist:901355
desc "creates database & database user"
task :create_database do
set :root_password, Capistrano::CLI.password_prompt("MySQL root password: ")
set :db_user, Capistrano::CLI.ui.ask("Application database user: ")
set :db_pass, Capistrano::CLI.password_prompt("Password: ")
set :db_name, Capistrano::CLI.ui.ask("Database name: ")
run "mysql --user=root --password=#{root_password} -e \"CREATE DATABASE IF NOT EXISTS #{db_name}\""
run "mysql --user=root --password=#{root_password} -e \"GRANT ALL PRIVILEGES ON #{db_name}.* TO '#{db_user}'@'localhost' IDENTIFIED BY '#{db_pass}' WITH GRANT OPTION\""
andhapp / can_destroy.rb
Created Feb 7, 2012 — forked from spalladino/can_destroy.rb
How to check if object can be destroyed if it has dependent restrict associations
View can_destroy.rb
class ActiveRecord::Base
def can_destroy?
self.class.reflect_on_all_associations.all? do |assoc|
assoc.options[:dependent] != :restrict || (assoc.macro == :has_one && self.send( || (assoc.macro == :has_many && self.send(
andhapp / callbacks.rb
Created Mar 26, 2012
capistrano pre-compile assets for rails 3.1
View callbacks.rb
desc "precompile the assets"
task :precompile_assets, :roles => :web, :except => { :no_release => true } do
run "cd #{current_path}; rm -rf public/assets/*"
run "cd #{current_path}; RAILS_ENV=production bundle exec rake assets:precompile"
andhapp / gist:2231347
Created Mar 28, 2012
Extract emails from google account
View gist:2231347
# Gemfile
source ""
gem 'mechanize'
gem 'hpricot'
# Ruby code
andhapp /
Created Apr 25, 2012 — forked from scottwb/
Monkey patches for a couple Rails Mime::Type.parse bugs.

Rails Mime::Type.parse Patches

There are two Rails issues in it's handling of the HTTP Accept header which cause a number of spurious exception emails via Airbrake. I am encountering this on Rails 3.0.7. One of these is fixed in a later version of Rails, but for other reasons I can't upgrade right now. The other bug is still present in Rails 3.2 and in master at the time of this writing. This gist includes some monkey patches you can apply to fix these issues until such time that they are fixed in Rails properly.

Rails Issue #736

Issue #736 is that Rails does not correctly parse a q-value in an Accept header when there is only one content-type specified. For example:

Accept: text/html;q=0.9
andhapp /
Created May 11, 2012 — forked from mikhailov/
Nginx+passenger application config: ssl redirection, http headers, passenger optimal settings. see details:
$ cd /usr/src
$ wget
$ tar xzvf ./nginx-0.8.52.tar.gz
$ rm ./nginx-0.8.52.tar.gz
$ gem install s3sync capistrano capistrano-ext passenger --no-ri --no-rdoc
$ passenger-install-nginx-module
# Automatically download and install Nginx? 2. No: I want to customize my Nginx installation
# Where is your Nginx source code located?: /usr/src/nginx-0.8.52
# Where do you want to install Nginx to?: /opt/nginx