Skip to content

Commit b3a035f

Browse files
committed
Optimize URI#hostname and URI#hostname=
1 parent 88eeea8 commit b3a035f

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

lib/uri/generic.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ def host=(v)
643643
#
644644
def hostname
645645
v = self.host
646-
/\A\[(.*)\]\z/ =~ v ? $1 : v
646+
v&.start_with?('[') && v.end_with?(']') ? v[1..-2] : v
647647
end
648648

649649
# Sets the host part of the URI as the argument with brackets for IPv6 addresses.
@@ -659,7 +659,7 @@ def hostname
659659
# it is wrapped with brackets.
660660
#
661661
def hostname=(v)
662-
v = "[#{v}]" if /\A\[.*\]\z/ !~ v && /:/ =~ v
662+
v = "[#{v}]" if v&.index(':') && !v.start_with?('[') && !v.end_with?(']')
663663
self.host = v
664664
end
665665

0 commit comments

Comments
 (0)