Skip to content

Instantly share code, notes, and snippets.

@aharpole
aharpole / README.md
Last active August 29, 2015 13:56
CC validation refactoring exercise

You need RSpec installed to run these tests.

gem install rspec
rspec credit_card_spec.rb

The CreditCard class is currently working but the code is in need of some refactoring. Refactor it (ensuring that the specs continue to pass) and when you are finished, share the gist with us.

Feel free to include in your gist a README containing an explanation of your refactorings and any thoughts or philosophies behind what you did. We're particularly interested in understanding what you personally consider to be good code, and this exercise will help us see into that a bit.

@aharpole
aharpole / atom_builder_example.rb
Created February 22, 2014 03:59
Using Rails Atom builder with non-AR objects
atom_feed do |feed|
feed.title "Some title"
feed.updated Time.now
@feed_entries.each do |item|
feed.entry item, {id:42, url:"http://this_object.com/url"} do |entry|
#code for entry here
end
end
end
@aharpole
aharpole / gist:11385660
Last active August 29, 2015 14:00
CSV.parse unexpected behavior
1.9.3-p545 :002 > headers = "col,is,cool"
=> "col,is,cool"
1.9.3-p545 :003 > csv = CSV.parse(headers,{headers:true})
=> #<CSV::Table mode:col_or_row row_count:1>
1.9.3-p545 :004 > tworows = "#{headers}\nabc,def,ghi"
=> "col,is,cool\nabc,def,ghi"
1.9.3-p545 :005 > other_csv = CSV.parse(tworows,{headers:true})
=> #<CSV::Table mode:col_or_row row_count:2>
1.9.3-p545 :006 > csv.headers
=> [] #I expect this to be ["col", "is", "cool"]
@aharpole
aharpole / csv.rb
Created April 28, 2014 23:00
headers method in CSV.rb in ruby/ruby trunk
#
# Returns the headers for the first row of this table (assumed to match all
# other rows). An empty Array is returned for empty tables.
#
def headers
if @table.empty?
Array.new
else
@table.first.headers
end
@aharpole
aharpole / rescue_nil.rb
Created May 30, 2014 02:24
safe use of rescue nil
def chunky_peanut_butter
EndlessChunkyPBSupplier.grab(:chunky_pb)
end
def smooth_peanut_butter
Cabinet.fetch(:smooth_peanut_butter) rescue nil
end
def make_pb_and_j
pb = smooth_peanut_butter || chunky_peanut_butter
@aharpole
aharpole / Gemfile
Created August 18, 2014 20:41
Pinger - a simple tool that you can run on Heroku that will ping a bunch of sites on your behalf
source 'http://rubygems.org'
gem "httparty"
@aharpole
aharpole / ntsb_software.md
Created September 1, 2014 20:55
Why software doesn't have the equivalent of NTSB crash ratings

from Matt Drance:

Struggling to explain to a friend why there’s no software equivalent to NTSB crash ratings. “It’s complicated” is honest, but not acceptable

The explanation is pretty straightforward. Cars are pretty uniform. They have changed a lot, but fundamentally, they are used the same as they have been for over a century, they fail in very consistent ways, and we've thus been able to make some good well-defined tests to determine how safe a car is in the event of a crash.

Software isn't like that. Web applications may have started to follow some consistent structure, and we have some patterns we follow pretty commonly (like talking over HTTP), but the fact remains that the apps and services are super un-uniform.

That poses a real difficulty if you want to develop some standardized tests. It would be pretty easy to develop some standardized tests for an app that only spoke HTTP and used a RESTful protocol to talk to one specific web browser u

@aharpole
aharpole / 93325f68.rb
Created September 5, 2014 05:53
before/after a massive controller refactoring
def update
p "Update is being called for out_of_money for #{params[:opportunity]}" if !params[:out_of_money].blank?
@opportunity = Opportunity.find(params[:id])
@opportunity.talking_points_boilerplate = params[:talking_points_boilerplate] if params[:talking_points_boilerplate]
@opportunity.description_link_boilerplate = params[:description_link_boilerplate] if params[:description_link_boilerplate]
@opportunity.annotation_boilerplate = params[:annotation_boilerplate] if params[:annotation_boilerplate]
demographics_to_save = []
params.fetch(:demographics,{}).each do |d|
demographics_to_save << d[1]
end
@aharpole
aharpole / sm_candidates.txt
Created October 31, 2014 03:41
I had my Fancy Hands assistant research Santa Monica's city council candidates on their stances on the Time Warner/Comcast merger
Kevin Mckeown response which was the longest was, "...corporate mergers and buyouts are not under the
jurisdiction of municipalities. Some years ago the State of California even took away our ability to negotiate with internet/cable/phone providers over franchises...I helped write the long-term plan that put fiber into every utility trench dug in Santa Monica for the past almost 20 years. As a result, Santa Monica is uniquely positioned to provide 100-Gigabit internet through a fiber network we control locally, which means no throttling and absolute net neutrality...The next phase of the plan is to bring this municipally-provided extreme-bandwidth fiber internet to individual residential households. Over the years I’ve been on Council, we’ve twice tried to establish city-wide wifi...I realize I’ve gone a bit beyond the initial question about corporate
internet providers. I hope to make them far less relevant for Santa Monicans, as they do not serve us well and show no sign of getting better." This is a shor
@aharpole
aharpole / script.js
Created March 27, 2015 21:03
a Greasemonkey script for Tower users to Open in Tower using SSH instead of HTTPS
// ==UserScript==
// @name GitHub open in desktop ssh
// @namespace 1427488342954
// @version 0.1
// @updateURL
// @downloadURL
// @include https://github.com/*
// ==/UserScript==
$(document).ready(function() {