Ruby: How to execute only one if condition in a loop? -


i trying have 1 of if conditions print.

subnet = ['10.14.1.32/27', '10.17.8.2/29'] system = gets.chomp subnet.each |cidr|   address_space = netaddr::cidr.create(cidr)   if address_space.contains? system     puts "found"   else     puts "not found"   end end 

the output 10.17.8.3 be

not found found 

how 1 response? tried using breaks doesn't work. all!

the trick use enumerable#find method:

subnet = ['10.14.1.32/27', '10.17.8.2/29'].collect |cidr|   netaddr::cidr.create(cidr) end  system = gets.chomp  found = subnet.find |cidr|   cidr.contains?(system) end  if (found)   puts "found" else   puts "not found" end 

that return either cidr object matched or nil if nothing did.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -