From: Arun Kumar Date: 2009-04-07T13:31:43+09:00 Subject: Re: Code duplication > 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 Thanks Ellie,You gave me a clue of not only solving the code duplication but also about handling the redirects. Thanks a lot. Regards Arun -- Posted via http://www.ruby-forum.com/.