aboutsummaryrefslogtreecommitdiffstats
path: root/lib/net
diff options
context:
space:
mode:
authorJosh Nichols <josh.nichols@gusto.com>2023-08-13 12:55:29 -0400
committergit <svn-admin@ruby-lang.org>2023-08-16 01:39:08 +0000
commit0300ea5a6c8a7a49feed73318fc8a991aa89fcfc (patch)
treef6f524a9e485d9c76d0f86d0fcb11207e76f0d2f /lib/net
parent8d985b1855d5d5dca88edce32625440a6c123a16 (diff)
downloadruby-0300ea5a6c8a7a49feed73318fc8a991aa89fcfc.tar.gz
[ruby/net-http] Improve performance of HTTPHeader#content_type
In the existing implementation, `main_type` and `sub_type` would end up being called multiple times potentially. Instead of doing that, save the result so it can be re-used. https://github.com/ruby/net-http/commit/179976f7ea
Diffstat (limited to 'lib/net')
-rw-r--r--lib/net/http/header.rb12
1 files changed, 8 insertions, 4 deletions
diff --git a/lib/net/http/header.rb b/lib/net/http/header.rb
index b704860c90..ec5552f04d 100644
--- a/lib/net/http/header.rb
+++ b/lib/net/http/header.rb
@@ -699,10 +699,14 @@ module Net::HTTPHeader
# res.content_type # => "application/json"
#
def content_type
- return nil unless main_type()
- if sub_type()
- then "#{main_type()}/#{sub_type()}"
- else main_type()
+ main = main_type()
+ return nil unless main
+
+ sub = sub_type()
+ if sub
+ "#{main}/#{sub}"
+ else
+ main
end
end