Skip to content
View 20160307172445_change_paperclip_attachment_path.rb
class MoveAttachmentsToNewLocation < ActiveRecord::Migration
def initialize(name =, version = nil)
access_key = Rails.application.secrets.g3_access_key_id
secret_key = Rails.application.secrets.g3_secret_access_key
storage = google_storage_access_key_id: access_key,
google_storage_secret_access_key: secret_key
@bucket_name = Rails.application.secrets.g3_bucket
@bucket = storage.directories.get(@bucket_name)
super(name, version)

Let's say you have a model, with an files attached, using Paperclip. You have a couple millions of those files and you're not sure that every one of them (and all its thumbnails) are still used by a database record.

You could use this rake task to recursively scan all the directories and check if the files need to be kept or destroyed.

In this example, the model is called Picture, the attachment is image and the path is partitioned like images/001/412/497/actual_file.jpg

The task is going down the path. Each time the path ends with 3 triplets of digits ("001/412/497" for example) it looks for a record with the ID 1412497. If such a record doesn't exist, the whole directory is moved to a parallel images_deleted directory. At the end you can delete the files if you like, or move them to an archive location.

You can use the "dry run" mode : to print which files would be removed

View ams_jsonapi.rb
# config/initializers/active_model_serializers.rb
ActiveModelSerializers.config.key_transform = :unaltered
ActiveModelSerializers.config.schema_path = 'docs/schema/schemata'
ActiveSupport.on_load(:action_controller) do
require 'active_model_serializers/register_jsonapi_renderer'
# Routing constraint:
# Request 'Content-Type' must be 'application/vnd.api+json'
View globals.rb
global_variables.sort.each do |name|
puts "#{name}: #{eval "#{name}.inspect"}"
View logfmt.rb
# Based on logfmt:
# For more complete implementation see:
# see
# For usage see:
# For Ruby parser see:
View The Three Go Landmines.markdown

There are three easy to make mistakes in go. I present them here in the way they are often found in the wild, not in the way that is easiest to understand.

All three of these mistakes have been made in Kubernetes code, getting past code review at least once each that I know of.

  1. Loop variables are scoped outside the loop.

What do these lines do? Make predictions and then scroll down.

func print(pi *int) { fmt.Println(*pi) }
View convert-emarref-3374193.bash
# Copied from
# Favicon and Apple Touch Icon Generator.
# Copyright 2012 @emarref
# Copyright 2012 Tom Vincent <>
# From:
# This bash script takes an image as a parameter, and uses ImageMagick to
# convert it to several other formats used on modern websites. The following
# copies are generated:
View unrelased_ams_08.patch
From 64ed05c484dc0add53183579a347b13d138ee944 Mon Sep 17 00:00:00 2001
From: Santiago Pastorino <>
Date: Tue, 7 May 2013 17:51:56 -0700
Subject: [PATCH 01/66] Define serializer as DefaultSerializer if not set
lib/active_model/array_serializer.rb | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/lib/active_model/array_serializer.rb b/lib/active_model/array_serializer.rb
View bench.rb
require 'benchmark/ips'
require 'securerandom'
Benchmark.ips do |x|
C = 'C'.freeze
DATA = (1..1_000_000).map { SecureRandom.hex(1) }"hex.chr") do |times|
i = 0
while i < times
View binary.rb
class Node
include Enumerable
include Comparable
attr_accessor :data, :left, :right
def initialize(data)
@data = data
Something went wrong with that request. Please try again.