Skip to content

Instantly share code, notes, and snippets.

View michaelfeathers's full-sized avatar

Michael Feathers michaelfeathers

View GitHub Profile
# MF: Handle 'last method of class' case
FileName = ARGV[0]
MethodName = ARGV[1]
def numbered_lines(file_name)
lines = []
File.open(file_name).each_line {|line| lines << line }
#!/usr/bin/env ruby
# Print the vaportrail of a ruby file in a git repo, i.e.,
# the complexity level at each commit.
#
# Requires: >= bash ?.?
# >= git 1.7.1
# >= ruby 1.9.2
# >= flog 2.5.0
#
#!/usr/bin/env ruby
# Print the complexity over time for a Ruby method in a
# git repository.
#
# Requires: >= bash ?.?
# >= git 1.7.1
# >= ruby 1.9.2
# >= flog 2.5.0
#
@michaelfeathers
michaelfeathers / methodshark.rb
Created March 9, 2011 15:58
Output the complexity trend of an individual method across its history
#!/usr/bin/env ruby
# Print the complexity over time for a Ruby method in a
# git repository.
#
# Requires: >= bash ?.?
# >= git 1.7.1
# >= ruby 1.9.2
# >= flog 2.5.0
#
@michaelfeathers
michaelfeathers / classtrend.rb
Created April 9, 2011 13:28
Print the complexities of all methods over time for a class in a git repo.
#!/usr/bin/env ruby
# Print the complexities of all methods over time
# for a class in a git repo.
#
# Requires: bash
# >= git 1.7.1
# >= ruby 1.9.2
# >= flog 2.5.0
@michaelfeathers
michaelfeathers / gccmock.rb
Created May 5, 2011 19:39
Generate stubs that tell you when they are called. Helpful when componentizing for test.
# gccmock - generate exploding link stubs from linker error messages
#
# Usage: gcc [files] 2>&1 ruby gccmock.rb > [filename].c
LINK_SYMBOL = /.*"_([_a-z0-9]*)",/
puts "#include <assert.h>\n"
puts "#define EXPLODE(name) void name() { assert(!\"unexpected call\"); }"
puts ARGF.select { |line| LINK_SYMBOL =~ line } \
.map { |line| LINK_SYMBOL.match(line)[1] } \
@michaelfeathers
michaelfeathers / gist:1083654
Created July 14, 2011 22:48
Node test pattern
sys = require('sys');
function Cell(x,y) {
this.x = x;
this.y = y;
this.state = (((x - 10) * (x - 10) + (y - 10) * (y - 10)) % 5) == 0
}
Cell.prototype.run = function() {
if (this.state == true)
def perform
@results ||= {}
puts "Looking up query"
query = Query.find_by_id(options["query_id"])
engine = Quote::Engine.create(query)
puts "Finding quotes"
quotes = engine.find_quotes(options["options"])
puts "Found quotes"
# NOTE:
# This is here so that sorting and filtering can take advantage
@michaelfeathers
michaelfeathers / gist:1316333
Created October 26, 2011 13:23
Ruby in Haskell style
# :: [event] -> [month,int]
def avg_lines_per_commit_by_month events
cls_by_month = lines_added_per_commit(events).group_by {|date,_| month_from_date(date) }
cls_by_month.map {|_,cls| cls.map {|cl| cl[1]}.mean }.flatten
end
# :: [event] -> Float
def percent_reduction method_events
non_deleted = method_events.select {|e| e.status != :deleted }
return 0.0 if non_deleted.count == 0
-- Modeled on the behavior of https://github.com/burkhartt/Tim.SentenceParser/blob/master/Tim.SentenceParser/Tim.SentenceParser/SentenceParser.cs
import Char
withoutLastWord :: String -> String
withoutLastWord = unwords . init . words
sentenceBefore :: String -> Int -> String
sentenceBefore "" _ = ""
sentenceBefore _ 0 = ""