diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb
index 1022e5d1d459474892092b4b22fd004e52ebbe67..1544e6765460aaf3499b584a2a309c76ad6986c1 100644
--- a/Library/Homebrew/diagnostic.rb
+++ b/Library/Homebrew/diagnostic.rb
@@ -100,7 +100,7 @@ module Homebrew
 
       # See https://github.com/Homebrew/legacy-homebrew/pull/9986
       def check_path_for_trailing_slashes
-        bad_paths = PATH.new(ENV["PATH"]).select { |p| p[-1..-1] == "/" }
+        bad_paths = PATH.new(ENV["PATH"]).select { |p| p.end_with?("/") }
         return if bad_paths.empty?
 
         inject_file_list bad_paths, <<-EOS.undent
diff --git a/Library/Homebrew/extend/ENV/shared.rb b/Library/Homebrew/extend/ENV/shared.rb
index 6fa9b77780f2a92b105c87b0f4de46f651badd4f..b51ade48b9c6fe276babc3778ef536a83e8e1fc7 100644
--- a/Library/Homebrew/extend/ENV/shared.rb
+++ b/Library/Homebrew/extend/ENV/shared.rb
@@ -197,22 +197,23 @@ module SharedEnvExtension
 
   # @private
   def userpaths!
-    paths = PATH.new(self["PATH"]).to_a
-    # put Superenv.bin and opt path at the first
-    new_paths = paths.select { |p| p.start_with?("#{HOMEBREW_REPOSITORY}/Library/ENV", "#{HOMEBREW_PREFIX}/opt") }
-    # XXX hot fix to prefer brewed stuff (e.g. python) over /usr/bin.
-    new_paths << "#{HOMEBREW_PREFIX}/bin"
-    # reset of self["PATH"]
-    new_paths += paths
-    # user paths
-    new_paths += ORIGINAL_PATHS.map do |p|
-      begin
-        p.realpath.to_s
-      rescue
-        nil
-      end
-    end - %w[/usr/X11/bin /opt/X11/bin]
-    self["PATH"] = PATH.new(new_paths.uniq)
+    path = PATH.new(self["PATH"]).select do |p|
+      # put Superenv.bin and opt path at the first
+      p.start_with?("#{HOMEBREW_REPOSITORY}/Library/ENV", "#{HOMEBREW_PREFIX}/opt")
+    end
+    path.append(HOMEBREW_PREFIX/"bin") # XXX hot fix to prefer brewed stuff (e.g. python) over /usr/bin.
+    path.append(self["PATH"]) # reset of self["PATH"]
+    path.append(
+      # user paths
+      ORIGINAL_PATHS.map do |p|
+        begin
+          p.realpath.to_s
+        rescue
+          nil
+        end
+      end - %w[/usr/X11/bin /opt/X11/bin],
+    )
+    self["PATH"] = path
   end
 
   def fortran