From: Eleanor McHugh Date: 2009-03-01T08:22:07+09:00 Subject: Re: Gets the broadcast address of an IP address On 28 Feb 2009, at 22:49, Hassan Schroeder wrote: > On Sat, Feb 28, 2009 at 11:12 AM, Eleanor McHugh > wrote: > >>> ... But is there a way to get automatically the >>> "192.168.0.255" address thanks to a Ruby method? I know I can get >>> my IP >>> with IPSocket.getaddress(Socket.gethostname) but I don't see any >>> way to >>> gets its broadcast address. > >> The simplest way to do it is to use a regex to change the last term >> in the >> quad: >> >> IPSocket.getaddress(Socket.gethostname).gsub!(/.\d{1,3}$/, '.255') > > Simple but not necessarily correct :-) > > > True, but I'm not sure the general case is so easy on the eye ;p subnet_mask = [ 255, 255, 255, 0 ] IPSocket .getaddress(Socket.gethostname).split('.').zip(subnet_mask).collect { | (i, m)| ((~m) & 255) | i.to_i }.join(".") Ellie Eleanor McHugh Games With Brains http://slides.games-with-brains.net ---- raise ArgumentError unless @reality.responds_to? :reason