Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def to_i

# Returns a string containing the IP address representation.
def to_s
str = to_string
str = _to_string(@addr)
return str if ipv4?

str.gsub!(/\b0{1,3}([\da-f]+)\b/i, '\1')
Expand All @@ -227,6 +227,7 @@ def to_s
str = sprintf('::%s%d.%d.%d.%d', $1, $2.hex / 256, $2.hex % 256, $3.hex / 256, $3.hex % 256)
end

str << @zone_id if @zone_id
str
end

Expand Down
8 changes: 8 additions & 0 deletions test/test_ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ def test_to_s
assert_equal("3ffe:505:2::1", IPAddr.new("3ffe:505:2::1").to_s)
end

def test_to_s_with_zone_id
assert_equal("::%eth0", IPAddr.new("::%eth0").to_s)
assert_equal("::ffff:192.168.1.2%eth0", IPAddr.new("::ffff:192.168.1.2%eth0").to_s)
assert_equal("::192.168.1.2%eth0", IPAddr.new("::192.168.1.2%eth0").to_s)
assert_equal("fe80::1%0ab", IPAddr.new("fe80::1%0ab").to_s)
assert_equal("fe80::1%0012", IPAddr.new("fe80::1%0012").to_s)
end

def test_as_json
assert_equal("192.168.1.2", IPAddr.new("192.168.1.2").as_json)
assert_equal("192.168.1.0/24", IPAddr.new("192.168.1.2/24").as_json)
Expand Down
Loading