Skip to content

Instantly share code, notes, and snippets.

def pairs(participants)
pairs = participants.shuffle.each_slice(2).to_a
if pairs.last.size == 1
loner = pairs.pop
pairs.last << loner.first
end
pairs
end
class FriendlyGuid
attr_reader :mask
def initialize(mask)
@mask = mask
end
def guid(id)
(id.to_i ^ mask).to_s(36)
end
@georgebrock
georgebrock / Rakefile
Created February 18, 2010 23:32
Rake tasks to manage Heroku deploys
namespace :deploy do
PRODUCTION_APP = 'myapp'
STAGING_APP = 'myapp-staging'
def run(*cmd)
system(*cmd)
raise "Command #{cmd.inspect} failed!" unless $?.success?
end
def confirm(message)
@JangoSteve
JangoSteve / thread_safe_block_depth.rb
Created October 25, 2010 21:29
A way to track the current block level for instance block-methods, which is completely thread-safe
class MyObject
def block_depth=(value)
Thread.current[:block_depth] = value
end
def block_depth
Thread.current[:block_depth] || 0
end
@martinhawkins
martinhawkins / date_convert.rb
Created December 9, 2010 12:57
Contains the date_of_next method
#date_convert.rb
require 'date'
require 'active_support/core_ext'
Date::DATE_FORMATS.merge!({british: "%d/%m/%Y"})
class DateConvert
# usage:
# date_of_next( day_name, date) where day_name is a string or symbol representation of a full weekday name, e.g. :saturday
# and date is either a valid Date object or a string representation of a date
def forked!
ActiveRecord::Base.connection.reconnect!
Rails.cache.instance_variable_get(:@data).reset
Mongoid.config.master.connection.close
load File.join(RAILS_ROOT, 'config/initializers/mongoid.rb')
end
def run(job)
runtime = Benchmark.realtime do
fork do
@jamestyj
jamestyj / rails_check.sh
Created September 27, 2011 20:35
Simple script that ensures Rails processes stay within specific memory limit.
#!/bin/bash
#
# This script is triggered by crontab every 5 minutes.
# Logs to /var/log/ui-server/<host>-rails_check.log.
log_dir=/var/log/my_rails_app
mkdir -p $log_dir
log_file=$log_dir/$(hostname)-rails_check.log
# Limits in MB
@TheEmpty
TheEmpty / ControllerFormAttributes.rb
Created March 11, 2012 02:23
Trying some different ways to secure a Rails application with user input
module ControllerFormAttributes
@@types = {}
# form_params_accessors(:user, :email, :password, :password_confirmation)
def form_params_accessors(type, attributes)
@@types[type] = attributes
end
# user.update_attributes(form_params_for(:user))
def form_params_for(type)
@olegykz
olegykz / deploy.rb
Created November 15, 2012 15:34 — forked from stympy/deploy.rb
Skip asset pre-compilation when deploying if the assets didn't change
namespace :deploy do
namespace :assets do
task :precompile, :roles => :web, :except => { :no_release => true } do
begin
from = source.next_revision(current_revision) # <-- Fail here at first-time deploy because of current/REVISION absence
rescue
err_no = true
end
if err_no || capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}

On Mac OS X:

  • Install puppet 3.0.2, facter 1.6.17 and hiera 1.1.2
  • create /etc/puppet/puppet.conf
  • configure launchd puppet daemon
  • initial puppet run