On Mon, Mar 24, 2008 at 10:14 PM, Gi Ga <gigatavu / gmail.com> wrote:
> Can somebody write this for ruby?
>
>  <?php
>
>  echo getscore ("ruby", "lang_en");
>
>  function getscore($wrd, $lng) {
>   $wrd = str_replace(" ", "+", $wrd);
>   $url = "http://www.google.com/search?q=%22" . $wrd . "%22&num=1&lr=" .
>  $lng;
>   $pg = file_get_contents($url);
>   $sta = strpos($pg, "swrnum") + 7;
>   $end = $sta + 10;
>   $result = substr($pg, $sta, 10);
>   return (int)$result;
>  }
>
>  ?>

require 'open-uri'
require 'uri'

def get_score(wrd, lng)
	url = "http://www.google.com/search?q=%22#{URI.escape(wrd)}%22&num=1&lr=#{URI.escape(lng)}"
 	pg = open(url) {|io| io.read}
	return $1.to_i if pg =~ /swrnum=(\d+)/
	0 # or any default value
end

puts get_score("ruby", "lang_en")