Skip to content

Instantly share code, notes, and snippets.

View michaelfeathers's full-sized avatar

Michael Feathers michaelfeathers

View GitHub Profile
@michaelfeathers
michaelfeathers / gist:1855765
Created February 17, 2012 22:19
Five lines that turn Ruby into a different language
class Object
def method_missing m, *args
Object.respond_to?(m, true) ? Object.send(m, self, *args) : super
end
end
@michaelfeathers
michaelfeathers / gist:2413318
Created April 18, 2012 12:34
Each versus Reduce
/* On Array */
VALUE
rb_ary_each(ary)
VALUE ary;
{
long i;
for (i=0; i<RARRAY(ary)->len; i++) {
import Data.List
lineBreak :: String -> String
lineBreak = joinBrokenLines . joinWordsInBrokenLines . brokenLines . words
brokenLines :: [String] -> [[String]]
brokenLines [] = []
brokenLines wordList = brokenLine : brokenLines remainingWords
where (brokenLine, remainingWords) = splitAt (brokenLineWordCount wordList) wordList
@michaelfeathers
michaelfeathers / gist:3827233
Created October 3, 2012 14:33
Code volume
# A poor man's complexity measure for a file or group of files
puts ARGF.lines.grep(/^([ \t]*)/) { $1 }.map(&:length).reduce(0, :+)
@michaelfeathers
michaelfeathers / gist:3832740
Created October 4, 2012 10:19
Descending sort of Ruby files in a repository by number of commits
git log --name-only --no-merges | grep \.rb$ | sort | uniq -c | sort -nr
@michaelfeathers
michaelfeathers / gist:3837149
Created October 4, 2012 23:39
Turbulence Light - generates a turbulence dataset for a Ruby git repository
def complexity filename
File.read(filename).lines.grep(/^([ \t]*)/) { $1 }.map(&:length).reduce(0, :+)
end
commit_filenames = `git log --name-only | grep \.rb$`.lines \
.map(&:strip) \
.reject { |fn| fn.include? "_spec" } \
.reject { |fn| fn.include? "vendor" }
@michaelfeathers
michaelfeathers / gist:3838607
Created October 5, 2012 07:45
Turbulence Light-er
def complexity filename
File.read(filename).lines.grep(/^([ \t]*)/) { $1 }.map(&:length).reduce(0, :+)
end
filenames = `git log --name-only | grep \.rb$`.lines \
.map(&:strip) \
.reject { |fn| fn.include? "_spec" } \
.reject { |fn| fn.include? "vendor" }
require 'ostruct'
require 'date'
class OrdersReport < Struct.new(:orders,:date_range)
def total_sales_within_date_range
orders.select {|order| date_range.include?(order.placed_at) }
.map(&:amount)
.reduce(0.0,:+)
end
end
@michaelfeathers
michaelfeathers / gist:4185398
Created December 1, 2012 21:55
Sometimes you just need to zip two hashes
def zip_hash hash_a, hash_b, missing_element = nil
all_keys = (hash_a.keys + hash_b.keys).uniq
result = {}
all_keys.each do |key|
result[key] = [hash_a[key] || missing_element, hash_b[key] || missing_element]
end
result
end
# :: [[a,b]] -> (a..a) -> b -> [[a,b]]]
def spread mappings, range, default_value = 0
occupied = Hash[mappings]
range.map { |index| [index, occupied[index] || default_value] }
end