From: Eleanor McHugh Date: 2009-04-07T00:15:28+09:00 Subject: Re: Code duplication On 6 Apr 2009, at 14:50, Arun Kumar wrote: > My tutor is saying that there is a duplication in the above code. ie. > code for html reading is specified twice without any purpose and it > should be removed. I've no idea where there is a mistake. I'm a newbee > to ruby and i don't understand the problem correctly or where things > went wrong. Can anyone please help me to find the mistake. There are probably better solutions, but the following illustrates the point your tutor is making: MOZILLA_HEADER = { "User-Agent"=>"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)" } def get_http_response uri, max_redirects = 0 Net::HTTP.start(uri) do |connection| response = connection.get(uri.path, MOZILLA_HEADER) response &&= case response when Net::HTTPRedirection if max_redirects > 0 then get_http_response URI.parse(response['Location']), (max_redirects - 1) else raise "Too many redirects" end when Net::HTTPRedirection get_http_response URI.parse("http://#{uri.host}:#{uri.port}/"), max_redirects end end end data = get_http_response(my_uri, 3).body See how get_http_response is recursive in the case of an erroneous response? This minimises the actual HTTP interaction code as well as elegantly handling redirects. Whilst this could result in many more http connections being used, it also makes them clear up after themselves which is always good. Ellie Eleanor McHugh Games With Brains http://slides.games-with-brains.net ---- raise ArgumentError unless @reality.responds_to? :reason