Some helpful methods:

$ irb
irb(main):001:0> ip1 = "192.168.42.1"
=> "192.168.42.1"
irb(main):002:0> ip2 = "192.168.125.64"
=> "192.168.125.64"
irb(main):003:0> ip1.split(".")
=> ["192", "168", "42", "1"]
irb(main):004:0> ip2.split(".")
=> ["192", "168", "125", "64"]
irb(main):005:0> ip1.split(".")[0..1]
=> ["192", "168"]
irb(main):006:0> ip1.split(".")[0..1] == ip2.split(".")[0..1]
=> true

martin

On Mon, Dec 12, 2011 at 3:41 PM, Chaim Keren-Tzion
<chaim / intercomp.co.il> wrote:
> Hi,
> I've just started using Ruby last week and have a somewhat complex task to
> complete in a short amount of time and nobody local to ask.
>
> Here is what I'm trying to find:
> Given two arrays containing IP addresses like the ones below, how would I
> search for the first element in 'servers' that pattern matches and element
> in 'nics' where only the first 2 parts of the IP addresses need to match,
> like 192.168.*.* or 10.14.*.* or 98.139.*.*
>
> servers = Array["192.168.0.251","10.14.0.142","98.139.180.149"]
> nics = Array["10.10.0.255","173.194.37.16","10.14.0.170"]
>
> I've got some kind of beginning with the code below but I'm stuck. Any
> hints?
>
> ----------------
> log_server = "nothing"
>
> until log_server != "nothing" servers.each |thisip| do
>
> # Consider only the first two parts of the 'thisip' IP address (192.168.*.*
> or 10.14.*.* or 98.139.*.*)
> # Compare it with each element in 'nics'
> # Set 'log_server' equal to the first value of 'thisip' that pattern
> matches an element in 'nics'
> log_server = thisip
>
> end
> ----------------
>
> Thanks in advance
> Chaim