From ad087f8fac02c9de49ef78263a376d20e442ac2a Mon Sep 17 00:00:00 2001
From: Mike Mackintosh <m@zyp.io>
Date: Mon, 23 Mar 2015 16:55:21 -0400
Subject: [PATCH] added IPv4#[]= to fix #24

---
 lib/ipaddress/ipv4.rb       | 15 +++++++++++++++
 test/ipaddress/ipv4_test.rb | 14 ++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb
index 500531a..7d8e0d3 100644
--- a/lib/ipaddress/ipv4.rb
+++ b/lib/ipaddress/ipv4.rb
@@ -286,6 +286,21 @@ module IPAddress;
       @octets[index]
     end
     alias_method :octet, :[]
+
+    #
+    # Updated the octet specified at index
+    #
+    #   ip = IPAddress("172.16.100.50/24")
+    #   ip[2] = 200
+    #
+    #   #=>  #<IPAddress::IPv4:0x00000000000000 @address="172.16.200.1", 
+    #   #=>       @prefix=32, @octets=[172, 16, 200, 1], @u32=2886780929>
+    #
+    def []=(index, value)
+      @octets[index] = value.to_i
+      initialize("#{@octets.join('.')}/#{prefix}")
+    end
+    alias_method :octet=, :[]=
     
     #
     # Returns the address portion of an IP in binary format,
diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb
index 9f92c94..19264e2 100644
--- a/test/ipaddress/ipv4_test.rb
+++ b/test/ipaddress/ipv4_test.rb
@@ -581,6 +581,20 @@ class IPv4Test < Minitest::Test
     end
   end
 
+  def test_octect_updates
+    ip = @klass.new("10.0.1.15/32")
+    ip[1] = 15
+    assert_equal "10.15.1.15/32", ip.to_string
+
+    ip = @klass.new("172.16.100.1")
+    ip[3] = 200
+    assert_equal "172.16.100.200/32", ip.to_string
+
+    ip = @klass.new("192.168.199.0/24")
+    ip[2] = 200
+    assert_equal "192.168.200.0/24", ip.to_string
+  end
+
 end # class IPv4Test
 
   
-- 
GitLab