ruby-changes:2138
From: ko1@a...
Date: 5 Oct 2007 04:04:12 +0900
Subject: [ruby-changes:2138] knu - Ruby:r13629 (trunk): * lib/ipaddr.rb (in_addr, in6_addr, addr_mask): Make some minor
knu 2007-10-05 04:03:56 +0900 (Fri, 05 Oct 2007) New Revision: 13629 Modified files: trunk/ChangeLog trunk/lib/ipaddr.rb Log: * lib/ipaddr.rb (in_addr, in6_addr, addr_mask): Make some minor code optimization. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/ipaddr.rb?r1=13629&r2=13628 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=13629&r2=13628 Index: ChangeLog =================================================================== --- ChangeLog (revision 13628) +++ ChangeLog (revision 13629) @@ -1,3 +1,8 @@ +Fri Oct 5 04:02:39 2007 Akinori MUSHA <knu@i...> + + * lib/ipaddr.rb (in_addr, in6_addr, addr_mask): Make some minor + code optimization. + Fri Oct 5 03:25:51 2007 Akinori MUSHA <knu@i...> * lib/ipaddr.rb (<=>): Implement IPAddr#<=> and make IPAddr Index: lib/ipaddr.rb =================================================================== --- lib/ipaddr.rb (revision 13628) +++ lib/ipaddr.rb (revision 13629) @@ -496,12 +496,9 @@ def in_addr(addr) if addr =~ /^\d+\.\d+\.\d+\.\d+$/ - n = 0 - addr.split('.').each { |i| - n <<= 8 - n += i.to_i + return addr.split('.').inject(0) { |i, s| + i << 8 | s.to_i } - return n end return nil end @@ -525,25 +522,20 @@ if rest < 0 return nil end - a = [l, Array.new(rest, '0'), r].flatten! - n = 0 - a.each { |i| - n <<= 16 - n += i.hex + return (l + Array.new(rest, '0') + r).inject(0) { |i, s| + i << 16 | s.hex } - return n end def addr_mask(addr) case @family when Socket::AF_INET - addr &= IN4MASK + return addr & IN4MASK when Socket::AF_INET6 - addr &= IN6MASK + return addr & IN6MASK else raise "unsupported address family" end - return addr end def _reverse -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml