4

我写了以下代码:

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

uri = URI("http://#{address.to_s}")我的if情况可以去排队吗

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

在上面的代码中变成真的了吗?如果我的条件为真,如何转到下一个 IP 地址并跳过当前地址?

是否可以在 Ruby 中执行类似标签和goto语句的操作?

4

2 回答 2

16
next if response.code == "301" || response.code == "400"
于 2012-02-21T13:28:53.107 回答
11

你想跳到下一个迭代,对吗?只需使用next.

于 2012-02-21T12:56:08.157 回答