Class: MatchData

Inherits:
Object
  • Object
show all
Defined in:
opal/opal/corelib/match_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexp, match_groups) ⇒ MatchData

Returns a new instance of MatchData



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'opal/opal/corelib/match_data.rb', line 4

def initialize(regexp, match_groups)
  $~          = self
  @regexp     = regexp
  @begin      = `match_groups.index`
  @string     = `match_groups.input`
  @pre_match  = `#@string.substr(0, regexp.lastIndex - match_groups[0].length)`
  @post_match = `#@string.substr(regexp.lastIndex)`
  @matches    = []

  %x{
    for (var i = 0, length = match_groups.length; i < length; i++) {
      var group = match_groups[i];

      if (group == null) {
        #@matches.push(nil);
      }
      else {
        #@matches.push(group);
      }
    }
  }
end

Instance Attribute Details

#post_matchObject (readonly)

Returns the value of attribute post_match



2
3
4
# File 'opal/opal/corelib/match_data.rb', line 2

def post_match
  @post_match
end

#pre_matchObject (readonly)

Returns the value of attribute pre_match



2
3
4
# File 'opal/opal/corelib/match_data.rb', line 2

def pre_match
  @pre_match
end

#regexpObject (readonly)

Returns the value of attribute regexp



2
3
4
# File 'opal/opal/corelib/match_data.rb', line 2

def regexp
  @regexp
end

#stringObject (readonly)

Returns the value of attribute string



2
3
4
# File 'opal/opal/corelib/match_data.rb', line 2

def string
  @string
end

Instance Method Details

#==(other) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'opal/opal/corelib/match_data.rb', line 31

def ==(other)
  return false unless MatchData === other

  `self.string == other.string` &&
  `self.regexp == other.regexp` &&
  `self.pre_match == other.pre_match` &&
  `self.post_match == other.post_match` &&
  `self.begin == other.begin`
end

#[](*args) ⇒ Object



27
28
29
# File 'opal/opal/corelib/match_data.rb', line 27

def [](*args)
  @matches[*args]
end

#begin(pos) ⇒ Object



41
42
43
44
45
46
47
# File 'opal/opal/corelib/match_data.rb', line 41

def begin(pos)
  if pos != 0 && pos != 1
    raise ArgumentError, 'MatchData#begin only supports 0th element'
  end

  @begin
end

#capturesObject



49
50
51
# File 'opal/opal/corelib/match_data.rb', line 49

def captures
  `#@matches.slice(1)`
end

#inspectObject



53
54
55
56
57
58
59
60
61
62
63
# File 'opal/opal/corelib/match_data.rb', line 53

def inspect
  %x{
    var str = "#<MatchData " + #{`#@matches[0]`.inspect};

    for (var i = 1, length = #@matches.length; i < length; i++) {
      str += " " + i + ":" + #{`#@matches[i]`.inspect};
    }

    return str + ">";
  }
end

#lengthObject Also known as: size



65
66
67
# File 'opal/opal/corelib/match_data.rb', line 65

def length
  `#@matches.length`
end

#to_aObject



71
72
73
# File 'opal/opal/corelib/match_data.rb', line 71

def to_a
  @matches
end

#to_sObject



75
76
77
# File 'opal/opal/corelib/match_data.rb', line 75

def to_s
  `#@matches[0]`
end

#values_at(*indexes) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'opal/opal/corelib/match_data.rb', line 79

def values_at(*indexes)
  %x{
    var values       = [],
        match_length = #@matches.length;

    for (var i = 0, length = indexes.length; i < length; i++) {
      var pos = indexes[i];

      if (pos >= 0) {
        values.push(#@matches[pos]);
      }
      else {
        pos += match_length;

        if (pos > 0) {
          values.push(#@matches[pos]);
        }
        else {
          values.push(nil);
        }
      }
    }

    return values;
  }
end