Search code examples
rubyarraysenumerable

check if a variable is in array in ruby


I have all my IPs in an array as below.

list_of_ips = Socket.ip_address_list.select{|intf| intf.ipv4?}

I am trying to use Enumerable include to check whether this array contains IP 192.168.1.27 which it does, but I get the return value as 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

I think I need to use to filter the array with ip_address somehow, but not sure how.


Solution

  • list_of_ips.any? {|ip| ip.ip_address == '192.168.1.27'}