4

I wrote the following code:

require 'net/http'
require 'uri'
require 'IPAddr'
require 'timeout'
require 'Resolv'
require 'open-uri'

#puts "Origin IP:\n\n"
#originip = gets()
(IPAddr.new("209.85.175.120")..IPAddr.new("209.85.175.150")).each do |address|
  uri = URI("http://#{address.to_s}")
  puts "#{uri}"
  http = Net::HTTP.new(uri.host, uri.port)
  puts "#{http}"
  Net::HTTP.start(uri.host, uri.port) do |http|
    request = Net::HTTP::Get.new uri.request_uri
    response = http.request request
    if response.code == "301" || response.code == "400" then

    end
    #request.initialize_http_header({"Origin" => "#{originip}"})
  end

Is it possible to go to line uri = URI("http://#{address.to_s}") in my if condition

if response.code == "301" || response.code == "400" then
 # condition
end

in the above code becomes true? How can I go to the next IP address and skip the current one if my condition becomes true?

Is it possible to do something like a label and goto statement in Ruby?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Akash Panchal
  • 205
  • 1
  • 6
  • 20

2 Answers2

16
next if response.code == "301" || response.code == "400"
Yoann Le Touche
  • 1,280
  • 9
  • 13
11

You want to skip to the next iteration, is that right? Just use next.

Alex D
  • 29,755
  • 7
  • 80
  • 126
  • no, want to skip current iteration if I get response as per my if condition and go to next one so that I dont get timeout error if one of IP in given range is not responding properly and I can go to next IP to perform required things – Akash Panchal Feb 21 '12 at 16:58
  • 1
    OK. `next` skips the current iteration, so you can just use that. – Alex D Feb 21 '12 at 17:41