From 2247b0becbb2ad0939645f460669f8351fc0e376 Mon Sep 17 00:00:00 2001 From: Brandon Weaver Date: Mon, 10 Nov 2025 21:58:41 -0800 Subject: [ruby/rubygems] Add documentation for pattern matching methods https://github.com/ruby/rubygems/commit/18f64c6b29 --- lib/rubygems/platform.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/rubygems/platform.rb b/lib/rubygems/platform.rb index 411512a465..f0eae47c54 100644 --- a/lib/rubygems/platform.rb +++ b/lib/rubygems/platform.rb @@ -146,8 +146,33 @@ class Gem::Platform to_a.compact.join(@cpu.nil? ? "" : "-") end + ## + # Deconstructs the platform into an array for pattern matching. + # Returns [cpu, os, version]. + # + # Gem::Platform.new("x86_64-linux").deconstruct #=> ["x86_64", "linux", nil] + # + # This enables array pattern matching: + # + # case Gem::Platform.new("arm64-darwin-21") + # in ["arm64", "darwin", version] + # # version => "21" + # end alias_method :deconstruct, :to_a + ## + # Deconstructs the platform into a hash for pattern matching. + # Returns a hash with keys +:cpu+, +:os+, and +:version+. + # + # Gem::Platform.new("x86_64-darwin-20").deconstruct_keys(nil) + # #=> { cpu: "x86_64", os: "darwin", version: "20" } + # + # This enables hash pattern matching: + # + # case Gem::Platform.new("x86_64-linux") + # in cpu: "x86_64", os: "linux" + # # Matches Linux on x86_64 + # end def deconstruct_keys(keys) { cpu: @cpu, os: @os, version: @version } end -- cgit v1.2.3