Skip to content

Instantly share code, notes, and snippets.

mattetti / gist:3458669
Created Aug 25, 2012
Instrument ActiveRecord and push the results to Statsd
View gist:3458669
SQL_PARSER_REGEXP = /^(\w+)\s(\w+)\s\W*(\w+)/
ActiveSupport::Notifications.subscribe "sql.active_record" do |name, start, finish, id, payload|
if payload[:name] == "SQL"
if Thread.current[:stats_context] # where I store the name of the request context
payload[:sql] =~ SQL_PARSER_REGEXP # $1 will be the query type, $3 the table
(finish - start) * 1000, 1)
hassox / benchmark.rb
Created Aug 30, 2012 — forked from jnicklas/benchmark.rb
Hash access speed with strings/vs symbols
View benchmark.rb
require "benchmark"
hash = {'key' => 1, :key => 2}
n = 5_000_000 do |x|"strings") { n.times { hash['key'] } }"symbols") { n.times { hash[:key] } }"strings, set") { n.times { hash['key'] = 1 } }
scotttam / apns.rb
Created Jan 31, 2012
Sends an Apple Push Notification with Ruby
View apns.rb
require "rubygems"
require "yajl"
require "openssl"
require "socket"
device_token = '39cac56f 986a0e66 3c4fd4f4 68df5598 024d2ca3 8b9f307c 741c180e 9fc30c62'
device_token = device_token.gsub(" ", "")
the_byte_token = [device_token].pack("H*")
file ="ruby_the_byte_token", "wb")

Inheritance is a key concept in most object-oriented languages, but applying it skillfully can be challenging in practice. Back in 1989, M. Sakkinen wrote a paper called Disciplined inheritance that addresses these problems and offers some useful criteria for working around them. Despite being more than two decades old, this paper is extremely relevant to the modern Ruby programmer.

Sakkinen's central point seems to be that most traditional uses of inheritance lead to poor encapsulation, bloated object contracts, and accidental namespace collisions. He provides two patterns for disciplined inheritance and suggests that by normalizing the way that we model things, we can apply these two patterns to a very wide range of scenarios. He goes on to show that code that conforms to these design rules can easily be modeled as ordinary object composition, exposing a solid alternative to tradi

nstielau / default.rb
Created May 18, 2011
A simple Chef recipe to install Jenkins
View default.rb
# Cookbook Name:: jenkins
# Recipe:: default
# This is super-simple, compared to the other Chef cookbook I found
# for Jenkins (
# This doesn't include Chef libraries for adding Jenkin's jobs via
myitcv / time_travel_trigger.sql
Last active Mar 8, 2022
Trigger-based equivalent of old PostgreSQL time travel module - see for more details
View time_travel_trigger.sql
Copyright (c) 2015 Paul Jolly <
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
devonestes / with_example.ex
Created Feb 8, 2020
Further refactoring of a with statement
View with_example.ex
# Step 1
def create_subscription(email, plan_id, payment_method_id) do
with %User{customer_id: nil, name: name} = user <-
Repo.get_by(User, email: email),
{:ok, %Stripe.Customer{id: customer_id}} <-
name: name,
email: email,
payment_method: payment_method_id,
henrik /
Last active May 23, 2022
Sandi Metz' four rules from Ruby Rogues episode 87. Listen or read the transcript:
  1. Your class can be no longer than 100 lines of code.
  2. Your methods can be no longer than five lines of code.
  3. You can pass no more than four parameters and you can’t just make it one big hash.
  4. When a call comes into your Rails controller, you can only instantiate one object to do whatever it is that needs to be done. And your view can only know about one instance variable.

You can break these rules if you can talk your pair into agreeing with you.

wvengen /
Last active Jul 8, 2022
Ruby memory analysis over time

Finding a Ruby memory leak using a time analysis

When developing a program in Ruby, you may sometimes encounter a memory leak. For a while now, Ruby has a facility to gather information about what objects are laying around: ObjectSpace.

There are several approaches one can take to debug a leak. This discusses a time-based approach, where a full memory dump is generated every, say, 5 minutes, during a time that the memory leak is showing up. Afterwards, one can look at all the objects, and find out which ones are staying around, causing the