KHAN! (Ruby)

This evening I read a story on reddit in which someone graphed the number results google gives vs. the number of A’s in “KHAN.”  KHAN! is from the second Star Trek Movie, “The Wrath of Khan.”  The original results from that article are here.  I thought it was pretty funny, so I decided to write a ruby script to automatically do the same thing (I’ve been using ruby a lot lately).

require 'gruff'
require 'open-uri'

dataToGraph = Array.new
khanNumOfAs = 1
maxKhanAs = 100

g = Gruff::Line.new
g.title = "KHAN!"

khanNumOfAs.upto(maxKhanAs){|i|
    sleep 0.5
    open('http://www.google.com/search?q=KH' + "A"*i + "N") do |f|
        f.each do |line|
            dataToGraph[i] = $~[1].gsub(/,/, "").to_i if /of about ([0-9,]+)/.match(line)
        end
    end
    print "KH#{"A"*i}N!!!!!    results: #{dataToGraph[i]}n"
}
g.data("KHAN!", dataToGraph)
g.write('khan.png')

Kevin helped me with the regular expressions to get the number of results out of google.

Final results, no log scale :-(

Final results, no log scale 🙁