Repository: ruby/rubygems (Ruby)

Issue:
Nonsense results from `gem install <nonexistent gem>` in some cases
<!--

Thank you for contributing to the
[rubygems](https://github.com/rubygems/rubygems) repository, and specifically to
[RubyGems](https://guides.rubygems.org/).

-->

### Describe the problem as clearly as you can

<!--

Replace this with an explanation of the problem you are having. Be as much clear and precise as you can.

-->

Sometimes `gem install <nonexistent gem>` will return results which are far less accurate than RubyGems is capable of providing.

E.g., things like `areospike-rails-support` or `capistrano-rails-synchronizer` in response to `gem install railss`.

### Did you try upgrading RubyGems?

<!--

Make sure you're using the latest version of both RubyGems by running `gem
update --system`.

It's likely that your issue has been fixed in recent versions, so just upgrading
might do the trick, and will also save us some time :)

-->

Using master.

### Post steps to reproduce the problem

1. Try to install a nonexistent gem which is very close to a real one (e.g. `railss`).
2. That's it.

### Which command did you run?

<!-- Replace this with the specific command that is causing trouble. -->

`gem install railss`

### What were you expecting to happen?

<!-- Replace this with the results you expected before running the command. -->

It to suggest things like "rails", "railsqs", "xrails", etc.

### What actually happened?

```
$ gem install railss
ERROR:  Could not find a valid gem 'railss' (>= 0) in any repository
ERROR:  Possible alternatives: aerospike-rails-support, akitaonrails-shift_subtitle, akupchanko-astrails-safe, angular_rails_seo, astrails-safe, authorized_rails_scaffolds, bootstrap-application-wizard-rails-sass, capistrano-rails-server, capistrano-rails-subdir, capistrano-rails-synchronizer
```

<!-- Replace this with the actual result you got. Paste the output of your command here. -->

### Run `gem env` and paste the output below

<!-- Replace this with the result of `gem env`. Don't forget to anonymize any private data! -->

<details>
<summary>`gem env`</summary>

```
RubyGems Environment:
  - RUBYGEMS VERSION: 3.6.0.dev
  - RUBY VERSION: 3.3.5 (2024-09-03 patchlevel 100) [x86_64-linux]
  - INSTALLATION DIRECTORY: /home/puppy/.gem/ruby/3.3.5
  - USER INSTALLATION DIRECTORY: /home/puppy/.gem/ruby/3.3.0
  - RUBYGEMS PREFIX: /home/puppy/dev/duckinator/rubygems
  - RUBY EXECUTABLE: /home/puppy/.rubies/ruby-3.3.5/bin/ruby
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /home/puppy/.gem/ruby/3.3.5/bin
  - SPEC CACHE DIRECTORY: /home/puppy/.cache/gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /home/puppy/.rubies/ruby-3.3.5/etc
  - RUBYGEMS PLATFORMS:
     - ruby
     - x86_64-linux
  - GEM PATHS:
     - /home/puppy/.gem/ruby/3.3.5
     - /home/puppy/.rubies/ruby-3.3.5/lib/ruby/gems/3.3.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => true
     - :bulk_threshold => 1000
     - "gem" => "--suggestions --no-document"
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /home/puppy/.gem/ruby/3.3.5/bin
     - /home/puppy/.rubies/ruby-3.3.5/lib/ruby/gems/3.3.0/bin
     - /home/puppy/.rubies/ruby-3.3.5/bin
     - /home/puppy/.cargo/bin
     - /home/puppy/bin
     - /home/puppy/.local/bin
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/local/games
     - /usr/games
     - /var/lib/flatpak/exports/bin
```

</details>




Fix (patch):
diff --git a/lib/rubygems/spec_fetcher.rb b/lib/rubygems/spec_fetcher.rb
index 610edf25c921..9c0399f19653 100644
--- a/lib/rubygems/spec_fetcher.rb
+++ b/lib/rubygems/spec_fetcher.rb
@@ -176,19 +176,12 @@ def suggest_gems_from_name(gem_name, type = :latest, num_results = 5)
 
     matches = names.map do |n|
       next unless n.match_platform?
-      [n.name, 0] if n.name.downcase.tr("_-", "").include?(gem_name)
+      distance = levenshtein_distance gem_name, n.name.downcase.tr("_-", "")
+      next if distance >= max
+      return [n.name] if distance == 0
+      [n.name, distance]
     end.compact
 
-    if matches.length < num_results
-      matches += names.map do |n|
-        next unless n.match_platform?
-        distance = levenshtein_distance gem_name, n.name.downcase.tr("_-", "")
-        next if distance >= max
-        return [n.name] if distance == 0
-        [n.name, distance]
-      end.compact
-    end
-
     matches = if matches.empty? && type != :prerelease
       suggest_gems_from_name gem_name, :prerelease
     else