Alex Rivera | Logout

Fastest way to find a String into an array of string

Asked 2012-02-16T15:25:49.687
20

The script has to verify if one predefined IP is present in a big array of IPs. Currently I code that function like this (saying that "ips" is my array of IP and "ip" is the predefined ip)

ips.each do |existsip|
  if ip == existsip
    puts "ip exists"
    return 1
  end
end
puts "ip doesn't exist"
return nil

Is there a faster way to do the same thing?

Edit : I might have wrongly expressed myself. I can do array.include? but what I'd like to know is : Is array.include? the method that will give me the fastest result?

Edit
Report

1 Answer

2

have you tried the Array#include? function?

http://ruby-doc.org/core-1.9.3/Array.html#method-i-include-3F

You can see from the source it does almost exactly the same thing, except natively.

answered 2012-02-16T15:29:23.193

Your Answer