diff options
author | bluemonk <ceresa@gmail.com> | 2010-08-27 11:02:28 +0200 |
---|---|---|
committer | bluemonk <ceresa@gmail.com> | 2010-08-27 11:02:28 +0200 |
commit | b21339bd7d1daf111d8c72f3c2e1d996e675b87e (patch) | |
tree | 63bed475a5ca02ed3a0fe96676b25fbba4276afa /lib | |
parent | 805a4bac180887798a43418972f1218305fa1fb6 (diff) | |
download | ipaddress-b21339bd7d1daf111d8c72f3c2e1d996e675b87e.tar.gz |
Added IPv4#private?
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ipaddress/ipv4.rb | 16 | ||||
-rw-r--r-- | lib/ipaddress/prefix.rb | 2 |
2 files changed, 17 insertions, 1 deletions
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb index 7db750c..3ef811a 100644 --- a/lib/ipaddress/ipv4.rb +++ b/lib/ipaddress/ipv4.rb @@ -539,6 +539,22 @@ module IPAddress; end # + # Checks if an IPv4 address objects belongs + # to a private network RFC1918 + # + # Example: + # + # ip = IPAddress "10.1.1.1/24" + # ip.private? + # #=> true + # + def private? + [self.class.new("10.0.0.0/8"), + self.class.new("172.16.0.0/12"), + self.class.new("192.168.0.0/16")].any? {|i| i.include? self} + end + + # # Returns the IP address in in-addr.arpa format # for DNS lookups # diff --git a/lib/ipaddress/prefix.rb b/lib/ipaddress/prefix.rb index 2e915d8..c881811 100644 --- a/lib/ipaddress/prefix.rb +++ b/lib/ipaddress/prefix.rb @@ -244,7 +244,7 @@ module IPAddress # #=> 340282366920938463444927863358058659840 # def to_u128 - eval "0b#{bits}.to_i" + bits.to_i(2) end end # class Prefix123 < Prefix |