Skip to content

Instantly share code, notes, and snippets.

geeksam /
Created February 20, 2015 19:03
OFFS Indentation example (OFFS:

I propose the following convention: the actual lines of code that are directly related to the OFFS gem itself should be aligned with the left margin. Code inside the would_like_to and may_still_need_to blocks should be indented "normally", as though the surrounding OFFS do/end lines were not present. See the example Ruby file also in this gist.

This convention serves two purposes:

  1. It provides a visual indication that something in the file is "weird", thus calling our attention to the complexity penalty being imposed by carrying extra branches in VCS.
  2. When the feature itself is eventually cleaned up, the diff will be a straight deletion, not the deletion-plus-insertion one would otherwise get from deindenting a block of code. This will make it more obvious if someone deletes one branch and makes an edit in the other in the same Git commit.
geeksam / gist:1128496
Created August 5, 2011 20:54
Platypus .rvmrc file
View gist:1128496
# PROJECT_RUBY="ruby-1.8.7-p302"
# use the global gemset in order to check for and install bundler, since we
# don't need it to be different for different projects
rvm use $PROJECT_RUBY@global --create --install
echo -n "Rubygems version 1.3.7 required. Do we have the right version?... "
if `ruby -rubygems -e "puts (Gem::VERSION == '$NEED_GEM_VERSION') ? exit(0) : exit(1)"`; then
geeksam / gist:1151929
Created August 17, 2011 16:24
gem_dep_tree output
View gist:1151929
rails 2.3.11 (>= 0)
actionmailer 2.3.11 (= 2.3.11)
actionpack 2.3.11 (= 2.3.11)
activesupport 2.3.11 (= 2.3.11)
rack 1.1.2 (~> 1.1.0)
activerecord 2.3.11 (= 2.3.11)
activeresource 2.3.11 (= 2.3.11)
rake 0.8.7 (>= 0.8.3)
rake 0.9.2 (>= 0.8.3)
rails 3.0.9 (>= 0)
geeksam / gist:1222811
Created September 16, 2011 18:53
View gist:1222811
class HashProxy
instance_methods.each { |meth| undef_method(meth) unless meth =~ /^__/ }
return delegate unless delegate.kind_of?(Hash)
def initialize(hash)
@hash = hash
geeksam / gist:1641464
Created January 19, 2012 17:59
Unsubscribe via snail mail? wtf?
View gist:1641464
If you'd prefer not to receive e-mail like this from Adobe in the future, please unsubscribe. Alternatively, you may mail your unsubscribe request to:
Adobe Systems Incorporated
Attn: Change of Address/Privacy
343 Preston Street
Ottawa, ON K1S 1N4
geeksam / gist:1689918
Created January 27, 2012 17:32
If Rails really, REALLY cared about OO principles
View gist:1689918
(In response to, with apologies and smoochies to @jc00ke.)
-- app
-- data_mappers <--- (see note below)
-- models
-- views
-- controllers
geeksam / gist:1881240
Created February 22, 2012 04:03
Slightly friendlier API for adding custom method traces to NewRelic
View gist:1881240
require 'new_relic/agent/method_tracer'
class NR
# Convenience method for adding NewRelic tracing around specific methods of interest.
# Note about performance:
# " CAUTION - adding probes to your code adds overhead.
# Each probe burns about 20 microseconds of CPU.
# Be careful not to probe a method that's called frequently in a loop. "
geeksam / gist:2560081
Created April 30, 2012 17:12
Epic Morning Commute
View gist:2560081
  • Check bus arrival times, see that bus closest to my house is due... NOW.
  • Sprint out door.
  • Sprint down the long block, catch bus.
  • Get off bus downtown, use Car2Go membership to drive Smart Car rest of way to office (just for fun).
  • Get to office ready to set up ergonomically appropriate desk and monitors.
  • Discover that only one monitor has arrived (no desk), without the VESA mount adapter needed to put it on my monitor stand.
  • Unable to do any physical setup, go to sit on couch in lobby area.
  • Realize that I left my RSA token at home, 2.8 miles away.
  • Discover that wifi is not working. (RSA token wouldn't have helped anyway.)
  • Pack up, go back outside to the Car2Go Smart Car I parked directly in front of the office just 20 minutes earlier. RFID reader pauses, then displays message NO CONNECTION / Car temporarily not available for rental

Rubyists! In the process of doing an "extract method object" refactoring, I wound up with a service object that calls several other (inner) service objects. A simplified version is below.

Note that (a) I'm using a simple Result object to report success/failure back to the calling code, and (b) I'm passing the same Result instance around to subservices (to keep track of overall process state).

My question is this: in a multistep process, how should I handle failure in an intermediate step?

The options I can think of are:

  1. As in the example below (do_phase_3 unless @result.failure?), explicitly check for failure in a substep, and abort manually.
  2. Raise an exception, which gets caught in the outer (class-level) .perform call, which then returns the result object as usual.
geeksam / gist:3336885
Created August 13, 2012 04:15
Testing bicycle interface (from POODR)
View gist:3336885

I really like the idea of testing the interface of the base Bicycle class, but I'm having that "someone on the Internet is WRONG!" feeling when I get to the section "Testing Concrete Subclass Behavior".

Specifically, you write "The RoadBikeTest should ensure that local_spares works while maintaining deliberate ignorance about the existence of the spares method. The shared BicycleInterfaceTest already proves that RoadBike responds correctly to spares, it is redundant and ultimately limiting to reference that method directly in this test."

The thing is, though, that--at least as written in the Rough Cut version of the book I'm looking at--the BicycleInterfaceTest doesn't prove that RoadBike responds correctly to spares. It only proves that it responds to spares.

So I went off to recreate the code locally to make sure that what I was seeing was correct, and sure enough, I can comment out the entire method body of Bicycle#spares, and all the interface tests pass because they're just checking #respon