Feature #21675
openAdvent of Pattern Matching
Description
Ruby pattern matching is a very useful feature, but many of the core classes to not leverage it currently. Given this I would like to directly invest in opening PRs to add this functionality and see how many cases we can reasonably add as we approach Ruby 4.0.
I have already started here:
-
Gem::Version- https://github.com/ruby/ruby/pull/15109 -
Net::HTTPResponse- https://github.com/ruby/ruby/pull/15110 -
Net::HTTPGenericRequest- https://github.com/ruby/ruby/pull/15111
But what I am currently considering looking at first are:
-
Pathname- Path manipulation is ubiquitous. Pattern matching on path components (dirname, basename, extname)
would simplify file handling logic. -
URI::Generic- URL routing and parsing would benefit from matching on scheme, host, port, and path components. -
IPAddr- Network programming would benefit from matching on IP address components and families for filtering
and routing logic. -
File::Stat- File metadata inspection is common. Matching on size, mode, timestamps, and file type predicates
would simplify permission and type checks. -
Process::Status- Process exit status checking is verbose. Matching on exitstatus, signals, and success state
would clarify intent. -
Addrinfo- Socket programming would benefit from matching on address family, socket type, and IP address/port
for connection handling. -
OpenSSL::X509::Certificate- Certificate validation logic could be cleaner with matching on subject, issuer,
and validity dates. -
OpenSSL::X509::Name- Distinguished name parsing for certificate handling would benefit from component
extraction. -
Gem::Requirement- Complements Gem::Version for dependency management patterns. -
Gem::Specification- Gem metadata inspection in tooling could benefit from structured matching.
If this is of interest I will start looking for more areas as well. My goal is to make this a feature that is well supported throughout common libraries in Ruby.
We can use this issue as a potential staging ground for discussion of what else might make sense.
Updated by baweaver (Brandon Weaver) 1 day ago
Net::HTTP¶
I think this provides good coverage so far, but might look into others later:
- Add pattern matching support to Net::HTTPGenericRequest
- Add pattern matching support to Net::HTTPResponse
- Add pattern matching support to Net::HTTP
- Add pattern matching support to Net::HTTPExceptions
RubyGems¶
Just starting in on some of these, though the Gem::Version was what kicked this off initially in a side-bar conversation.
Updated by der-scheme (Sebastian Dufner) about 1 hour ago
ยท Edited
I added Exception#deconstruct to core.
Next up is deconstruct_keys.
I also noticed that neither Enumerable nor Set implement deconstruct. These have issues as in consuming Enumerable can have side effects, and that an Array pattern would enforce an order of elements on the Set that it has no influence over, but I think they would be nice to have regardless. Edit: Matz doesn't like it because of the issues mentioned.