Skip to content

Instantly share code, notes, and snippets.

Created April 2, 2012 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2284925 to your computer and use it in GitHub Desktop.
Save anonymous/2284925 to your computer and use it in GitHub Desktop.
Cast a HN Poll to a Ruby Hash
%w{rubygems hpricot open-uri}.each{|r|require r}
class Array
def to_int_hash
h={};each_with_index{|e,i| h[e]=self[i+1].gsub(/a-z/,'').to_i if i.even?};h
end
end
def poll_to_hash(url)
doc=Hpricot(open(url))
items=[]
(doc/'/html/body/center/table/tr[3]/td/table[1]/tr[6]/td[2]/table/tr').each{|x|items<<"#{x.inner_text}" unless x.inner_text==''};nil
items.to_int_hash
end
p poll_to_hash('http://news.ycombinator.com/item?id=3788271')
p poll_to_hash('http://news.ycombinator.com/item?id=3786926')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment