检查变量是否在ruby中的数组中

vik*_*027 1 ruby arrays enumerable

我将所有IP都放在一个数组中,如下所示.

list_of_ips = Socket.ip_address_list.select{|intf| intf.ipv4?}
Run Code Online (Sandbox Code Playgroud)

我想使用可枚举包括检查该数组是否包含IP 192.168.1.27这是这样,但我得到的返回值设置为false.

puts list_of_ips[1].ip_address  ## This prints 192.168.1.27
puts $this_is_my_ip             ## This prints 192.168.1.27

puts list_of_ips.include? '192.168.1.27'  ## Gives me false
Run Code Online (Sandbox Code Playgroud)

我想我需要用某种方式用ip_address过滤数组,但不知道如何.

San*_*osh 6

list_of_ips.any? {|ip| ip.ip_address == '192.168.1.27'}
Run Code Online (Sandbox Code Playgroud)