Class: MatchData

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

Overview

Extension to the MatchData class This class adds additional functionality to the MatchData class, allowing conversion to a structured format.

Examples:

Converting MatchData to OpenStruct

match_data = /(?<name>\w+)/.match("John")
struct = match_data.to_struct

Instance Method Summary collapse

Instance Method Details

#to_hashHash

Converts the MatchData object to a hash. captures as values.

Examples:

Converting MatchData to a hash

match_data = /(?<name>\w+)/.match("John")
hash = match_data.to_hash

Returns:

  • (Hash)

    A hash representation of the MatchData, with names as keys and



22
23
24
25
26
# File 'documented/common/class_exts/matchdata.rb', line 22

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

#to_structOpenStruct

Converts the MatchData object to an OpenStruct.

Returns:

  • (OpenStruct)

    An OpenStruct representation of the MatchData.



12
13
14
# File 'documented/common/class_exts/matchdata.rb', line 12

def to_struct
  OpenStruct.new to_hash
end