Skip to content

Instantly share code, notes, and snippets.

View stevenharman's full-sized avatar

Steven Harman stevenharman

View GitHub Profile
@coreyhaines
coreyhaines / .rspec
Last active April 11, 2024 00:19
Active Record Spec Helper - Loading just active record
--colour
-I app
@panthomakos
panthomakos / benchmark.rb
Created May 3, 2012 20:06
Benchmark Your Bundle
#!/usr/bin/env ruby
require 'benchmark'
REGEXPS = [
/^no such file to load -- (.+)$/i,
/^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
/^Missing API definition file in (.+)$/i,
/^cannot load such file -- (.+)$/i,
]
@sj26
sj26 / 0-readme.md
Created May 5, 2012 05:39 — forked from burke/0-readme.md
ruby-1.9.3-p194 cumulative performance patch.

Patched ruby 1.9.3-p194 for 30% faster rails boot

What is?

This script installs a patched version of ruby 1.9.3-p194 with patches for boot-time performance improvements (#66 and #68), and runtime performance improvements (#83 and #84). It also includes the new backported GC from ruby-trunk.

Huge thanks to funny-falcon for the performance patches.

@shakefu
shakefu / example.sh
Last active August 30, 2022 01:22
Bash Script with Short and Long Options
# Option defaults
OPT="value"
# getopts string
# This string needs to be updated with the single character options (e.g. -f)
opts="fvo:"
# Gets the command name without path
cmd(){ echo `basename $0`; }
@hellerbarde
hellerbarde / latency.markdown
Created May 31, 2012 13:16 — forked from jboner/latency.txt
Latency numbers every programmer should know

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@knewter
knewter / food_fight_play_comands_controller.rb
Created August 31, 2012 23:52
command controller with a callback style
class FoodFightPlayCommandsController < LoggedInController
def create
command = FoodFightPlayCommand.new(params[:food_fight_play_command])
command.person_id = current_person.id
# Set up success / failure callbacks
command.on_success = method(:on_success)
command.on_failure = method(:on_failure)
command.execute!
end
@steveklabnik
steveklabnik / some_controller.rb
Created October 14, 2012 01:07
Some thoughts on real ViewModels for Draper 2.0
class SomeController < AC::Base
def show
foo = Foo.first
bar = Bar.first
show_model = ShowViewModel.new(foo, bar)
render show_model
end
end
@stevenharman
stevenharman / heroku-signals-ticket.md
Last active November 9, 2018 11:12
Sending user signals to Heroku workers/process...

me:

Is it possible to send a signal to a worker/process? I realize the platform sends SIGTERM and SIGKILL when restarting a dyno, but I need to send a USR1 to one of my workers to tell it to stop picking up new jobs. Normally this is achieved via kill -USR1 <pid>, but on the Heroku platform not only do we not know know the pid, we also don't run one-off commands on the same dyno.

Caio (heroku support):

We had this feature experimentally at some point but it was never productized. I recommend you find other ways to signal your processes, like setting a database flag.

me:

@blowmage
blowmage / tasks_controller_refactoring.rb
Created November 12, 2012 20:17 — forked from ryanb/tasks_controller_refactoring.rb
Variation of RubyTapas episode "021 Domain Model Events" without using callbacks
class TasksController < ApplicationController
def update
if @task.update_attributes(params[:task])
tracker = PostSaveTaskTracker.new(@task)
TaskPusher.new(tracker, socket_id).push_changes
TaskMailSender.new(tracker, current_user).deliver_email
# success response
else
# failure respond
end
@ryanb
ryanb / issues_with_modules.md
Created November 29, 2012 22:38
Points on how modules can make code difficult to read.

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article &lt; ActiveRecord::Base