Shell History with Ruby Golf

11:56 AM EDT Friday, April 11 2008

I saw this post about Shell History and thought why not try to do it in Ruby. Probably inspired by the fact that the Masters underway, this became an exercise in Code Golf. Here's what I came up with:

~ $ history | ruby -e '$<.inject(Hash.new(0)){|h,l| h[l.split[1]] += 1; h}.sort{
    |a,b| b[1]<=>a[1]}[0..9].each{|k,v| puts "#{v} #{k}"}'
138 cd
55 ls
40 merb
31 mate
29 cat
19 mv
17 httperf
15 rm
14 ant
14 rake

I must admit, the fact that I can't fit in on one line in the blog makes me feel slightly defeated. Can you do better?

Posted in  | Tags Golf, Ruby, ShellHistory | 0 Comments