Skip to content

Instantly share code, notes, and snippets.

Elements of Ruby Style

This is a scratchpad of style guidelines for Ruby. When undefined, there are multiple ways that are acceptable and dependent on context.

Returning values

  • Never use explicit return when intent is clear
  • Use as a short circuit guard statement
@jch
jch / .gitignore
Created December 13, 2011 04:26
MongoMapper::Sluggable, generate permalink-like slugs for document attributes
Gemfile.lock
@jch
jch / net_http_default_proxy.rb
Created December 14, 2011 19:56
make all net/http requests go through an http proxy
require 'net/http'
module Net
module DefaultProxy
# Defaults all requests through Net::HTTP through an http proxy.
# Example:
# require 'net/http'
# require 'net/http_extensions'
# Net::HTTP.set_http_proxy!
#
@jch
jch / mexican_chicken_tomato_soup.md
Created January 3, 2012 00:37
mexican chicken tomato soup
  • cilantro (after cooking)
  • onion (before and after, small cubes after for crunch and sharp)
  • tomatos
  • celery
  • chicken (strips, small)
  • carrots (cubed)
  • green bell pepper (cooked very soft)
  • zucchini

very thin consistency, strong tomato flavor (maybe canned tomatos with juice?). chicken broth and tomato base.

@jch
jch / hunt-down-execution-order-test-failures.md
Created January 9, 2012 20:19
Hunting Down Execution Order Test Failures

Hunting Down Execution Order Test Failures

Unit tests should pass when run in random order. But for an existing legacy project, certain tests might depend on the execution order. One test might run perfectly fine by itself, but fail miserably when run after another test. Rather than running different combinations manually, RSpec 2.8 has the option to run specs in random order with the --order random flag. But even with this, it can be hard to determine which specific test is causing the dependency. For example:

class << self
# Define an attr_accessor, but the reader method will
# set the variable if there is a first argument
def dsl_accessor(*args)
args.each do |name|
class_eval <<-ACCESSORS
attr_writer :#{name}
def #{name}(*args)
send("#{name}=", args.first) if !args.empty?
@#{name}
@jch
jch / debug.sh
Created February 8, 2012 20:09
sample haproxy config
# http://danielmiessler.com/study/lsof/
sudo lsof -c haproxy
sudo lsof -iTCP | grep LISTEN
# http://danielmiessler.com/study/tcpdump/
# capture unlimited length, on loopback device, don't resolve hostname, absolute sequence numbers, tcp, src or dst port == 443
sudo tcpdump -s 0 -i lo0 -n -S tcp port 443
@jch
jch / rspec-performance.md
Created February 16, 2012 17:17
RSpec Performance Module

RSpec Performance Module

Port http://guides.rubyonrails.org/performance_testing.html to RSpec. Add as another example group or add to existing controller example group

describe SomeController, :performance => true do
  # someway to load seed data for these sets of tests
  fixtures      :production_dump
  use_database  :production
@jch
jch / git-filet.rb
Created February 29, 2012 05:21
find the commit that causes a regression. will bundle into a gem at some point
def commit_for(pathspec = "HEAD")
`git log -1 --format=%H #{pathspec}`.chomp
end
def oneline_for(pathspec = "HEAD")
`git log -1 --oneline #{args[:good]}`.chomp
end
# remember what HEAD is so we can jump back to it
START = commit_for('HEAD')
@jch
jch / faraday_middlewares.rb
Created March 7, 2012 21:40
newrelic and caching middlewares for faraday
require 'new_relic'
require 'faraday'
require 'active_support'
class InstrumentationMiddleware < Faraday::Middleware
extend NewRelic::Agent::MethodTracer
def call(env)
uri = env[:url]
method = env[:method]
metrics = ["External/#{uri.host}/Faraday/#{method}", "External/#{uri.host}/all"]