Class: MatchData

Inherits:
Object show all
Defined in:
lib/source/ruby.rb

Overview

MatchData is the class of the object returned by String#match, Regexp#match, and Regexp#last_match. It encapsulates all the results of a pattern match.

Instance Method Summary collapse

Constructor Details

#initializeMatchData

:nodoc:



3891
3892
3893
# File 'lib/source/ruby.rb', line 3891

def initialize # :nodoc:
  `this.__captures__=[]`
end

Instance Method Details

#[](*args) ⇒ Object

call-seq:

mtch[i]             -> object
mtch[start, length] -> array
mtch[range]         -> array

Match Reference – MatchData acts as an array, and may be accessed using the normal array indexing techniques. mtch[0] returns the entire matched string, while mtch[1], mtch[2], and so on return the values of the matched backreferences (portions of the pattern between parentheses).

m = /(.)(.)(\d+)(\d)/.match("THX1138.")

m[0]       #=> "HX1138"
m[1, 2]    #=> ["H", "X"]
m[1..3]    #=> ["H", "X", "113"]
m[-3, 2]   #=> ["X", "113"]


3913
3914
3915
# File 'lib/source/ruby.rb', line 3913

def [](*args)
  `c$Array.prototype.m$_brac.apply(this.__captures__,args)`
end

#begin(n) ⇒ Object

FIX: Incomplete



3918
3919
# File 'lib/source/ruby.rb', line 3918

def begin(n)
end

#capturesObject

call-seq:

mtch.captures -> array

Returns the array of captures.

m = /(.)(.)(\d+)(\d)/.match("THX1138.").captures

m[0]    #=> "H"
m[1]    #=> "X"
m[2]    #=> "113"
m[3]    #=> "8"


3933
3934
3935
# File 'lib/source/ruby.rb', line 3933

def captures
  `this.__captures__.slice(1)`
end

#end(n) ⇒ Object

FIX: Incomplete



3938
3939
# File 'lib/source/ruby.rb', line 3938

def end(n)
end

#inspectObject

:nodoc:



3955
3956
3957
# File 'lib/source/ruby.rb', line 3955

def inspect # :nodoc:
  `c$Object.prototype.m$to_s.apply(this)`
end

#lengthObject

call-seq:

mtch.length -> integer
mtch.size   -> integer

Returns the number of elements in the match array.

m = /(.)(.)(\d+)(\d)/.match("THX1138.")

m.length    #=> 5


3951
3952
3953
# File 'lib/source/ruby.rb', line 3951

def length
  `this.__captures__.length`
end

#offset(n) ⇒ Object

FIX: Incomplete



3960
3961
# File 'lib/source/ruby.rb', line 3960

def offset(n)
end

#post_matchObject

call-seq:

mtch.post_match -> string

Returns the portion of the original string after the current match.

m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")

m.post_match    #=> ": The Movie"


3972
3973
3974
# File 'lib/source/ruby.rb', line 3972

def post_match
  `this.__post__`
end

#pre_matchObject

call-seq:

mtch.pre_match -> string

Returns the portion of the original string before the current match.

m = /(.)(.)(\d+)(\d)/.match("THX1138.")

m.pre_match   #=> "T"


3985
3986
3987
# File 'lib/source/ruby.rb', line 3985

def pre_match
  `this.__pre__`
end

#selectObject

FIX: Incomplete



3990
3991
# File 'lib/source/ruby.rb', line 3990

def select
end

#sizeObject

call-seq:

mtch.length -> integer
mtch.size   -> integer

Returns the number of elements in the match array.

m = /(.)(.)(\d+)(\d)/.match("THX1138.")

m.size    #=> 5


4003
4004
4005
# File 'lib/source/ruby.rb', line 4003

def size
  `this.__captures__.length`
end

#stringObject

call-seq:

mtch.string -> string

Returns a copy of the string that was matched against a pattern to produce mtch.

m1 = /(.)(.)(\d+)(\d)/.match("THX1138.")
m2 = "THX1138.".match(/(.)(.)(\d+)(\d)/)

m1.string   #=> "THX1138."
m2.string   #=> "THX1138."


4019
4020
4021
# File 'lib/source/ruby.rb', line 4019

def string
  `$q(this.__string__)`
end

#to_aObject

call-seq:

mtch.to_a -> array

Returns the MatchData’s internal array.

m = /(.)(.)(\d+)(\d)/.match("THX1138.")

m.to_a    #=> ["HX1138", "H", "X", "113", "8"]


4032
4033
4034
# File 'lib/source/ruby.rb', line 4032

def to_a
  `this.__captures__`
end

#to_sObject

call-seq:

mtch.to_s -> string

Returns the entire matched string.

m = /(.)(.)(\d+)(\d)/.match("THX1138.")

m.to_s    #=> "HX1138"


4045
4046
4047
# File 'lib/source/ruby.rb', line 4045

def to_s
  `this.__captures__[0]`
end

#values_atObject

FIX: Incomplete



4050
4051
# File 'lib/source/ruby.rb', line 4050

def values_at
end