1

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.

2 Answers 2

6
list_of_ips.any? {|ip| ip.ip_address == '192.168.1.27'}
Sign up to request clarification or add additional context in comments.

1 Comment

Clever... Anything left ? :)
2

You need to transform the objects in the array temporarily to strings to compare them to strings. Try this:

list_of_ips.map {|addr| addr.ip_address}.include? '192.168.1.27'

3 Comments

Yes.. it is... Voted up.
list_of_ips.map(&:ip_address)
@max: I always forget that shortcut, even though I've worked with ruby for years. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.