Project

General

Profile

Actions

Feature #21190

closed

Proposal for the Deconstruct Method in the MatchData Class

Added by aristotelesbr (Aristóteles Costa) 11 days ago. Updated 11 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:121394]

Description

Context:

The MatchData class currently lacks the deconstruct method, which is necessary for extracting values from a string using pattern matching in Ruby.

Current Extraction Method

Currently, extraction can be done as follows:

result = /(\d{2})(\d{2})(\d{9})/.match("5586987654321")

puts result[1] # => "55"
puts result[2] # => "86"
puts result[3] # => "987654321"

Proposed Solution:

Implement the deconstruct method in the MatchData class to allow conversion of the MatchData object into an array, enabling deconstruction of its components.

class MatchData
  def deconstruct
    self.to_a
  end
end

result = /(\d{2})(\d{2})(\d{9})/.match("5586987654321")
result in [_ , country_code, area_code, number]

puts country_code  # => "55"
puts area_code     # => "86"
puts number        # => "987654321"

Related issues 1 (0 open1 closed)

Is duplicate of Ruby - Feature #18821: Expose Pattern Matching interfaces in core classesClosedActions
Actions

Also available in: Atom PDF

Like0
Like1Like0Like0Like1