Fastest way to find a String into an array of string

前端 未结 5 1692
被撕碎了的回忆
被撕碎了的回忆 2021-02-12 18:18

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

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-12 18:40

    You could use the Array#include method to return you a true/false.

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

    if ips.include?(ip) #=> true
      puts 'ip exists'
    else
      puts 'ip  doesn\'t exist'
    end
    

提交回复
热议问题