Skip to content

Instantly share code, notes, and snippets.

@staltz
staltz / introrx.md
Last active April 20, 2024 14:15
The introduction to Reactive Programming you've been missing
# MODEL
class Case < ActiveRecord::Base
include Eventable
has_many :tasks
concerning :Assignment do
def assign_to(new_owner:, details:)
transaction do
@myitcv
myitcv / time_travel_trigger.sql
Last active March 8, 2022 06:50
Trigger-based equivalent of old PostgreSQL time travel module - see https://blog.myitcv.io/2014/02/25/row-level-version-control-with-postgresql.html for more details
/*
Copyright (c) 2015 Paul Jolly <paul@myitcv.org.uk)
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:
@henrik
henrik / rules.md
Last active May 23, 2022 12:31
Sandi Metz' four rules from Ruby Rogues episode 87. Listen or read the transcript: http://rubyrogues.com/087-rr-book-clubpractical-object-oriented-design-in-ruby-with-sandi-metz/
  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.

@hassox
hassox / benchmark.rb
Created August 30, 2012 23:28 — forked from jnicklas/benchmark.rb
Hash access speed with strings/vs symbols
require "benchmark"
hash = {'key' => 1, :key => 2}
n = 5_000_000
Benchmark.bm do |x|
x.report("strings") { n.times { hash['key'] } }
x.report("symbols") { n.times { hash[:key] } }
x.report("strings, set") { n.times { hash['key'] = 1 } }
@mattetti
mattetti / gist:3458669
Created August 25, 2012 01:47
Instrument ActiveRecord and push the results to Statsd
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
Statsd.timing("#{Thread.current[:stats_context]}.sql.#{$3}.#{$1}.query_time",
(finish - start) * 1000, 1)
end
end

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

@scotttam
scotttam / apns.rb
Created January 31, 2012 15:51
Sends an Apple Push Notification with Ruby
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 = File.open("ruby_the_byte_token", "wb")
@nstielau
nstielau / default.rb
Created May 18, 2011 16:22
A simple Chef recipe to install Jenkins
#
# Cookbook Name:: jenkins
# Recipe:: default
#
# https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+Ubuntu
# This is super-simple, compared to the other Chef cookbook I found
# for Jenkins (https://github.com/fnichol/chef-jenkins).
#
# This doesn't include Chef libraries for adding Jenkin's jobs via