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::Platform does not support it. This makes platform-specific conditional logic more verbose than necessary, particularly when routing gem installations or filtering platforms.

The current approach requires multiple conditionals or case statements with equality checks:

case platform
when Gem::Platform.new("x86_64-linux")
 install_linux_x64
when Gem::Platform.new("arm64-darwin")
 install_macos_arm
end

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

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

Implementation:

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

This enables platform-based pattern matching:

case platform
in cpu: "x86_64", os: "linux"
 install_linux_x64
in cpu: "arm64", os: "darwin"
 install_macos_arm
end

Or extracting specific components:

platform in [cpu, os, version]
# cpu => "x86_64", os => "linux", version => nil

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


alias_method :deconstruct, :to_a

def deconstruct_keys(keys)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to #9060, could we add a few lines of documentation to provide a quick explanation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

@hsbt hsbt merged commit 707104b into ruby:master Nov 12, 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.

3 participants