Skip to content

Instantly share code, notes, and snippets.

View michaelfeathers's full-sized avatar

Michael Feathers michaelfeathers

View GitHub Profile
@michaelfeathers
michaelfeathers / gist:f52c48e2455a75374dc8
Created June 7, 2014 19:12
Audiobook scripting utility
# Accepts program file names on the command line and renders them to stdout as a sequence of spoken words
token_map = {
"[" => "left-square-bracket",
"]" => "right-square-bracket",
"!" => "exclamation-point",
"\\" => "backslash",
"#" => "pound sign",
"\"" => "double-quote",
@michaelfeathers
michaelfeathers / gist:242d0ad0baa57765ced5
Created June 7, 2014 19:46
Literate Chaining Style in Ruby
module Kernel; def c text; self; end; end
puts ARGF.c("all files as text") .read
.c("convert to enumerable") .chars
.c("contiguous alphas and puncts") .chunk {|ch| ch =~ /[[:alnum:]]/ ? :alone : true }
.c("merging identifiers and nums") .map {|key,values| key == :alone ? values.join : values }
.c("array of idents and puncts") .flatten(1)
@michaelfeathers
michaelfeathers / gist:56f34287dbb55dc0d4a2
Last active August 29, 2015 14:02
An example of Literate Chaining Style in Ruby. The Kernel#c method is a pass through that serves as a placeholder for a comment and a convenient place for tracing.
# Accepts program file names on the command line and renders
# their text to stdout as a sequence of spoken words
token_map = {
"[" => "left square-bracket",
"]" => "right square-bracket",
"!" => "exclamation-point",
"\\" => "back slash",
"#" => "pound sign",
"\"" => "double-quote",
STRING_COUNT = 6
def tab_column string, fret
["---" ] * (string - 1) +
[fret.ljust(3,'-')] +
["---" ] * (STRING_COUNT - string)
end
puts ARGF.each_line
.map(&:split)
require 'ap'
require 'ripper'
CLASS_TEXT = "class A; end; class B; end"
CLASS_SEXP = Ripper.sexp(CLASS_TEXT)
BIG_TEXT = "class A; def a; @a = b; end; def b; @d = a; @e = a; end; end; module B; def b; end; end"
BIG_SEXP = Ripper.sexp(BIG_TEXT)
@michaelfeathers
michaelfeathers / gist:1e2934ebb95560cddc3b
Created April 3, 2015 02:05
return a string that represents increases, decreases, and stable changes in a method's length
# :: [event] -> String -> String
def method_delta_line es, method_name
es.select {|e| e.method_name == method_name }
.map(&:method_length)
.each_cons(2)
.map {|c,n| ["^","v","-"][(c <=> n) + 1] }
.join
end
def span_count ary
([0] + ary).lazy
.each_cons(2)
.count {|c,n| c == 0 && n != 0 }
end
class Array
def prefixes
result = []
(0..size).each {|n| result << take(n) }
result
end
def indices value
map {|v| v == value ? 0 : 1 }
# 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
#