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

View reshaping_the_command_approach.rb
class Case < ActiveRecord::Base
include Eventable
has_many :tasks
concerning :Assignment do
def assign_to(new_owner:, details:)
transaction do
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:
PavelBezpalov / 1. ELK.install
Last active Mar 23, 2022
ELK Stack with Rails (Elasticsearch, Logstash, Kibana) on Ubuntu VPS
View 1. ELK.install
$ sudo apt-get update && sudo apt-get install default-jre
$ wget -qO - | sudo apt-key add -
$ echo "deb stable main" | sudo tee -a /etc/apt/sources.list.d/elasticsearch-2.x.list
$ sudo apt-get update && sudo apt-get install elasticsearch
$ sudo update-rc.d elasticsearch defaults 95 10
$ sudo service elasticsearch restart
$ sudo service elasticsearch status
mnutt / Instrument Anything in Rails
Created Sep 6, 2010
How to use Rails 3.0's new notification system to inject custom log events
View Instrument Anything in Rails

Instrument Anything in Rails 3

With Rails 3.0 released a few weeks ago I've migrated a few apps and I'm constantly finding useful new improvements. One such improvement is the ability to log anything in the same way that Rails internally logs ActiveRecord and ActionView. By default Rails 3 logs look slightly spiffier than those produced by Rails 2.3: (notice the second line has been cleaned up)

Started GET "/" for at Mon Sep 06 01:07:11 -0400 2010
  Processing by HomeController#index as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1

Rendered layouts/_nav.html.erb (363.4ms)