Class: MatchData

Inherits:
Object
  • Object
show all
Defined in:
documented/common/class_exts/matchdata.rb

Overview

Represents a match data object that contains information about a regular expression match.

Examples:

Creating a match data object

match_data = /\d+/.match("123").to_struct

Instance Method Summary collapse

Instance Method Details

#to_hashHash

Converts the match data to a hash.

Examples:

Converting to hash

hash = match_data.to_hash

Returns:

  • (Hash)

    A hash representation of the match data, with names as keys and captures as values.



18
19
20
21
22
# File 'documented/common/class_exts/matchdata.rb', line 18

def to_hash
  self.names.zip(self.captures.map(&:strip).map do |capture|
    if capture.is_i? then capture.to_i else capture end
  end).to_h
end

#to_structOpenStruct

Converts the match data to an OpenStruct object.

Examples:

Converting to OpenStruct

struct = match_data.to_struct

Returns:

  • (OpenStruct)

    An OpenStruct representation of the match data.



10
11
12
# File 'documented/common/class_exts/matchdata.rb', line 10

def to_struct
  OpenStruct.new to_hash
end