Skip to content

Instantly share code, notes, and snippets.

David Heinemeier Hansson dhh

Block or report user

Report or block dhh

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View human-v-machine.js
#!/usr/bin/env node
// Results from MacBook (Retina, 12-inch, Early 2016), 1.3 GHz Intel Core m7, macOS 10.12.3, Node 7.6.0
// let a = { z: 1 }
// human-friendly x 85,821,153 ops/sec ±1.81% (87 runs sampled)
// machine-friendly x 92,613,579 ops/sec ±1.89% (85 runs sampled)
// Fastest is machine-friendly
// let a = undefined
View dcp-converter.rb
#!/usr/bin/env ruby
# Requires that you have ./bin/dcpTool from
require 'rubygems'
require 'bundler/setup'
require 'nokogiri'
input_camera_model = ARGV[0] || "LEICA Q (Typ 116)"
output_camera_model = ARGV[1] || "LEICA M10"
View Shitty Recruiter Spam.txt
This is Neela, recruiter from Kellton Tech Solutions (Formerly Prosoft Technology Group, Inc)
We are an Information Technology and Business Consulting firm specializing in Project-based Solutions
and Professional Staffing Services. I just tried reaching you today as your profile has been one of
the best matches for an immediate job opportunity I have with my client. So, please have a look at
the job description and let me know your interest ASAP. I would really appreciate if you could send
me your most updated resume also.
Please Note:
dhh / comments_channel.rb
Last active Sep 26, 2016
On-boarding a specialized broadcast method in the channel itself
View comments_channel.rb
# Channel
class CommentsChannel < ApplicationCable::Channel
def self.broadcast_comment(comment)
broadcast_to comment.message, comment: CommentsController.render(
partial: 'comments/comment', locals: { comment: comment }
def follow(data)
View config_for.rb
module YourApp
class Application < Rails::Application
# Convenience for loading config/foo.yml for the current Rails env.
# Example:
# config/cleversafe.yml:
# production:
# url:
dhh / test_induced_design_damage.rb
Last active Dec 15, 2019
This is an extraction from Jim Weirich's "Decoupling from Rails" talk, which explained how to apply the hexagonal design pattern to make every layer of your application easily unit testable (without touching the database etc). It only seeks to extract a single method, the EmployeesController#create method, to illustrate the design damage that's …
View test_induced_design_damage.rb
# Original Rails controller and action
class EmployeesController < ApplicationController
def create
@employee =
redirect_to @employee, notice: "Employee #{} created"
render :new
View aggregator.rb
class Profile < ActiveModel::Aggregator
aggregate :user
aggregate :email
def combine
View GridCountsQuery.rb
class Customers::GridCountsController < ApplicationController
def index
render json:[:grid_counts])
class GridCountsQuery
class << self
def run
query.each_with_object( do |row, grid|
View user_authentications_controller.rb
class UserAuthenticationsController < AuthenticatedController
def create
if authenticate
redirect_to return_path
redirect_to back_path, error: authentication_error_message
View profanity.rb
# Option A: I'd start with this
class Micropost < ActiveRecord::Base
validate :no_profanity
def no_profanity
if user.minor? && profanity = profane_words_used_in_content
errors.add :content, "Profanity: '#{profanity.join(", ")}' not allowed!"
You can’t perform that action at this time.