In article < .com>,
m4dc4p <> wrote:
>I whipped a quick script to send queries to Google and scrape results
>if it's a valid "calculator" query. If you don't know what I mean, put
>"5 + 5" into the Google search box. I love the feature but hate having
>to open a web browser each time I want to use it. Thus, this script!
>
>I'm pretty much a n00b to Ruby so any feedback, improvements, comments
>are appreciated. Enjoy!
>
>--- cut here ---
>require 'net/http'
>require 'cgi'
>
>format_result = Proc.new { |s|
> s.gsub("<font size=-2>
></font>",",").gsub("×","x").gsub("<sup>","^").gsub(" </sup>", "")
>}
>
>matchExp = Regexp.new("<td><img
>src=/images/calc_img.gif></td><td> </td><td nowrap>(.*?) =
>(.*?)</b>")
>while ((! (print "Enter an expression (exit to quit): "; line =
>gets).nil?) && line.strip.upcase != "EXIT")
> resp =
>Net::HTTP.get_response(URI.parse("http://www.google.com/search?q=#{CGI.escape(line.strip)}"))
> if resp.code == "200"
> begin
> matches = matchExp.match(resp.body)
> puts "==>" + format_result.call(matches[2])
> rescue NoMethodError
> puts "==> Expression not understood."
> rescue
> puts "==> Expression not understood. (#{$!.class.inspect},
>#{$!.inspect})"
> end
> else
> puts "==> Response error: #{resp.code}"
> end
>end
>
Sorry for two replies to the same post, but I tried your code and I always
get Expression not understood:
Enter an expression (exit to quit): 20 + 20
==> Expression not understood.
Oh, and as a former boss of mine used to say often: "Rube Goldberg would be
proud" (of this very convoluted method for adding numbers.
Phil