Skip to content

Conversation

@baweaver
Copy link
Contributor

@baweaver baweaver commented Nov 9, 2025

What was the end-user or developer problem that led to this PR?

Ruby 2.7+ introduced pattern matching, but Gem::NameTuple does not support it. This makes gem index operations and version resolution more verbose than necessary, particularly when filtering or routing based on gem metadata.

The current approach requires multiple method calls and conditionals:

name_tuples.select do |nt|
 nt.name == "rails" && nt.platform == "ruby" && nt.version >= Gem::Version.new("7.0")
end

What is your fix for the problem, implemented in this PR?

Added deconstruct and deconstruct_keys methods to Gem::NameTuple to enable pattern matching syntax.

Implementation:

  • deconstruct aliases the existing to_a method for array pattern matching, returning [name, version, platform]
  • deconstruct_keys returns a hash with keys :name, :version, and :platform

This enables name tuple pattern matching:

case name_tuple
in name: "rails", version: Gem::Version[major: 7..], platform: "ruby"
 use_modern_features
in name: "rails", version:, platform:
 legacy_support(version, platform)
end

Or filtering with array patterns:

name_tuples.select { it in ["rails", Gem::Version[major: 7..], "ruby"] }

Make sure the following tasks are checked

  • Describe the problem / feature
  • Write tests for features and bug fixes
  • Write code to solve the problem
  • Make sure you follow the current code style and write meaningful commit messages without tags

@hsbt hsbt merged commit d020896 into ruby:master Nov 14, 2025
73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants