Skip to content

Instantly share code, notes, and snippets.

@michaelfeathers
Last active December 16, 2015 07:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelfeathers/5395996 to your computer and use it in GitHub Desktop.
Save michaelfeathers/5395996 to your computer and use it in GitHub Desktop.
Attempt to discern number of spaces used for an indent across a set of source files.
class Array
def freq_by &block
group_by(&block).map {|k,v| [k, v.count] }.sort_by(&:first)
end
def freq
freq_by {|e| e }
end
end
puts ARGF.lines.map {|l| l.split(/\S/).first }
.compact
.select {|s| s.length > 1 }
.select {|s| s.chars.all? {|c| c == ' ' }}
.freq
.sort_by {|_,c| -c }
.take(10)
.map {|p,_| p.length }
.sort
.each_cons(2)
.take_while {|l,r| l.gcd(r) > 1 }
.map(&:first)
.reduce(&:gcd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment