From: Zach Dennis Date: 2004-12-28T22:48:04+09:00 Subject: Re: verifying a network connection Thomas Metz wrote: > Hi, > > is there a way in Ruby to find out, whether my computer has built up a > network connection or not? > > I'm using Windows2000. > > Thank you for helping a Ruby-newbie. > Hi Thomas, A quick and easy solution is the following, which work on a windows machine. ip_str = `ipconfig` ip_str =~ /IP Address[^\d]*((\d+\.){3}\d+)/ ip_addr = $1 Note that those are backticks around "ipconfig". I haven't tested this for a machine that doesn't have a ip address, so I'll leave that up to you. You can shorten this by just saying: `ipconfig` =~ /IP Address[^\d]*((\d+\.){3}\d+)/ ip_addr = $1 Here's a quick explanation (I dont know if you're new to ruby or to programming, so bare with me). The parts of the regex are in quotes: - `ipconfig` executes a shell command, aka ipconfig! - /IP Address[^\d]*((\d+\.){3}\d+)/ is a regular expression meaning: - Find the word IP Address, "IP Address" - which is followed by a non-digit as many times as possible "[^\d]*" - until we find one or more digits "\d+" - (which is followed by a period) at least 3 times "\.{3}" - and is followed by one or more digits "\d+" - The parenthesis's are used to group the match HTH, Zach