diff options
| author | Brandon Weaver <keystonelemur@gmail.com> | 2025-11-10 21:58:41 -0800 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2025-11-12 22:23:10 +0000 |
| commit | 2247b0becbb2ad0939645f460669f8351fc0e376 (patch) | |
| tree | 85e2a3fdfce04ce4686db7f78449b0e61c728057 | |
| parent | 3efabc8355df489d1fa9717e7cb1b8daa8ddf92e (diff) | |
[ruby/rubygems] Add documentation for pattern matching methods
https://github.com/ruby/rubygems/commit/18f64c6b29
| -rw-r--r-- | lib/rubygems/platform.rb | 25 |
1 files changed, 25 insertions, 0 deletions
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 |
