summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarco Ceresa <ceresa@gmail.com>2015-02-02 14:42:42 +0100
committerMarco Ceresa <ceresa@gmail.com>2015-02-02 14:42:42 +0100
commitf2080750a409c90360db24653436f1d1d026fcd3 (patch)
treec35117f0d2714f1075b32b4da5fa30d8196b626e
parentce5b524748759d23790a710fd1adcca6a547d91d (diff)
parentfbfd5830736efe15e6de2dc9585af27cacab3cfd (diff)
downloadipaddress-f2080750a409c90360db24653436f1d1d026fcd3.tar.gz
Merge pull request #58 from mikemackintosh/add-to_h-method
Add IPv4#to_h
-rw-r--r--README.rdoc9
-rw-r--r--lib/ipaddress/ipv4.rb15
-rw-r--r--test/ipaddress/ipv4_test.rb15
3 files changed, 37 insertions, 2 deletions
diff --git a/README.rdoc b/README.rdoc
index 0419f6c..9f28191 100644
--- a/README.rdoc
+++ b/README.rdoc
@@ -29,6 +29,8 @@ IPAddress 0.8.0 has been tested on:
* jruby-1.6.1 [ linux-i386-java ]
* ruby-1.9.1-p431 [ i386 ]
* ruby-1.9.2-p180 [ i386 ]
+* ruby-2.0.0-p353 [ x86_64-darwin14.0.0 ]
+* ruby-2.1.3-p242 [ x86_64-darwin14.0.0 ]
If you want to collaborate feel
free to send a small report to my email address, or
@@ -318,12 +320,17 @@ transform it in data format using the IPv4#data method:
ip.data
#=> "\254\020\n\001"
-Finally, you can transform an IPv4 address into a format which is
+Also, you can transform an IPv4 address into a format which is
suitable to use in IPv4-IPv6 mapped addresses:
ip.to_ipv6
#=> "ac10:0a01"
+Finally, much like IPv4#to_ipv6 you can use to IPv4#to_h method to return a non-semicolon delineated string (useful with pcap/byte level usage):
+
+ ip.to_h
+ #=> "ac100a01"
+
=== Classful networks
IPAddress allows you to create and manipulate objects using the old
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb
index 716d4d9..a32a4a0 100644
--- a/lib/ipaddress/ipv4.rb
+++ b/lib/ipaddress/ipv4.rb
@@ -231,6 +231,21 @@ module IPAddress;
alias_method :to_u32, :u32
#
+ # Returns the address portion in
+ # hex
+ #
+ # ip = IPAddress("10.0.0.0")
+ #
+ # ip.to_h
+ # #=> 0a000000
+ #
+ def hex(space=true)
+ "%.4x%.4x" % [to_u32].pack("N").unpack("nn")
+ end
+ alias_method :to_h, :hex
+ alias_method :to_hex, :hex
+
+ #
# Returns the address portion of an IPv4 object
# in a network byte order format.
#
diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb
index 5177c75..534349e 100644
--- a/test/ipaddress/ipv4_test.rb
+++ b/test/ipaddress/ipv4_test.rb
@@ -34,7 +34,13 @@ class IPv4Test < Test::Unit::TestCase
"172.16.0.0/16" => 2886729728,
"192.168.0.0/24" => 3232235520,
"192.168.100.4/30" => 3232261124}
-
+
+ @hex_values = {
+ "10.0.0.0" => "0a000000",
+ "172.16.5.4" => "ac100504",
+ "192.168.100.4" => "c0a86404",
+ }
+
@ip = @klass.new("172.16.10.1/24")
@network = @klass.new("172.16.10.0/24")
@@ -144,6 +150,13 @@ class IPv4Test < Test::Unit::TestCase
end
end
+ def test_method_to_hex
+ @hex_values.each do |addr,hex|
+ ip = @klass.new(addr)
+ assert_equal hex, ip.to_hex
+ end
+ end
+
def test_method_network?
assert_equal true, @network.network?
assert_equal false, @ip.network?