Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define MIME::Type#hash #167

Merged
merged 1 commit into from
Jul 4, 2022

Conversation

ajvondrak
Copy link
Contributor

@ajvondrak ajvondrak commented Jul 2, 2022

Fixes #166. See that issue for the relevant context.

Upon studying the definition of MIME::Type#eql?, I determined that we shouldn't include self.class in the hash. #eql? uses an #is_a? check, which will succeed even on subclasses of MIME::Type. So #eql? doesn't require self and other be of the exact same class. If we were to include self.class in the hash, then it would erroneously make the hashes different for two #eql? objects of different classes (since different classes generally hash to different values). I added an extra test around MIME::Type#eql? to codify this behavior. While I was in there, I also fixed a typo in an existing MIME::Type#eql? test. 😅

Note that there aren't many generic tests we can make around #hash itself. Even two distinct MIME::Type instances could still hash to the same value, in principle. So there are only really tests around instances that should be #eql? to each other.

One clue we have that this PR addresses the randomness of warnings seen in #163 is when I run rake with/without these changes locally. :)

On main, I see no warnings about the duplicate application/netcdf data, because the Set treats the two instances as distinct (unless we get lucky, which has also happened to me intermittently):

Without hash definition
$ git branch
  define-mime-type-hash
* main
$ rake
Run options: --seed 63590

# Running:

...................................................................................................................................................

Finished in 2.143452s, 68.5810 runs/s, 137.1619 assertions/s.

147 runs, 294 assertions, 0 failures, 0 errors, 0 skips
rm -rf doc
rm -r pkg

On the topic branch, I see all the warnings ‼️ now that the two instances hash the same:

With hash definition
$ git branch
* define-mime-type-hash
  main
$ rake
Type application/netcdf is already registered as a variant of application/netcdf.
Run options: --seed 24550

# Running:

.......Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
............................Type application/netcdf is already registered as a variant of application/netcdf.
.................Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
................Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
........Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.Type application/netcdf is already registered as a variant of application/netcdf.
.........Type application/netcdf is already registered as a variant of application/netcdf.
Type application/netcdf is already registered as a variant of application/netcdf.
...............................................

Finished in 3.228527s, 46.7706 runs/s, 92.3022 assertions/s.

151 runs, 298 assertions, 0 failures, 0 errors, 0 skips
rm -rf doc
rm -r pkg

This change is Reviewable

@halostatue halostatue merged commit 071107b into mime-types:main Jul 4, 2022
@halostatue
Copy link
Member

Thanks for the contribution! I’m going to delay releasing this a bit, as I would like to figure out how to resolve the load issue.

halostatue added a commit that referenced this pull request Jul 4, 2022
@ajvondrak ajvondrak deleted the define-mime-type-hash branch July 4, 2022 23:02
@ajvondrak
Copy link
Contributor Author

Makes sense. 👍 Glad I could help!

halostatue added a commit that referenced this pull request Aug 7, 2023
- 1 bug fix:

  - Added a definition of `MIME::Type#hash`. Contributed by Alex Vondrak
    in [#167][], fixing [#166][].

- Dependency and CI updates:

  - Update the .github/workflows/ci.yml workflow to test Ruby 3.2 and
    more reliably test certain combinations rather than depending on
    exclusions.

  - Change `.standard.yml` configuration to format for Ruby 2.3 as
    certain files are not properly detected with Ruby 2.0.

    - Change from `hoe-git` to `hoe-git2` to support Hoe version 4.

    - Apply `standardrb --fix`.

    - The above changes have resulted in the soft deprecation of Ruby
      versions below 2.6. Any errors reported for Ruby versions 2.0,
      2.1, 2.2, 2.3, 2.4, and 2.5 will be resolved, but maintaining CI
      for these versions is unsustainable.

[#166]: #166
[#167]: #167
@halostatue halostatue mentioned this pull request Aug 7, 2023
halostatue added a commit that referenced this pull request Aug 7, 2023
- 1 bug fix:

  - Added a definition of `MIME::Type#hash`. Contributed by Alex Vondrak
    in [#167][], fixing [#166][].

- Dependency and CI updates:

  - Update the .github/workflows/ci.yml workflow to test Ruby 3.2 and
    more reliably test certain combinations rather than depending on
    exclusions.

  - Change `.standard.yml` configuration to format for Ruby 2.3 as
    certain files are not properly detected with Ruby 2.0.

    - Change from `hoe-git` to `hoe-git2` to support Hoe version 4.

    - Apply `standardrb --fix`.

    - The above changes have resulted in the soft deprecation of Ruby
      versions below 2.6. Any errors reported for Ruby versions 2.0,
      2.1, 2.2, 2.3, 2.4, and 2.5 will be resolved, but maintaining CI
      for these versions is unsustainable.

[#166]: #166
[#167]: #167
halostatue added a commit that referenced this pull request Aug 7, 2023
- 1 bug fix:

  - Added a definition of `MIME::Type#hash`. Contributed by Alex Vondrak
    in [#167][], fixing [#166][].

- Dependency and CI updates:

  - Update the .github/workflows/ci.yml workflow to test Ruby 3.2 and
    more reliably test certain combinations rather than depending on
    exclusions.

  - Change `.standard.yml` configuration to format for Ruby 2.3 as
    certain files are not properly detected with Ruby 2.0.

    - Change from `hoe-git` to `hoe-git2` to support Hoe version 4.

    - Apply `standardrb --fix`.

    - The above changes have resulted in the soft deprecation of Ruby
      versions below 2.6. Any errors reported for Ruby versions 2.0,
      2.1, 2.2, 2.3, 2.4, and 2.5 will be resolved, but maintaining CI
      for these versions is unsustainable.

[#166]: #166
[#167]: #167
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MIME::Type defines #eql? but not #hash
2 participants