Class: Peruby::MatchState

Inherits:
Object
  • Object
show all
Defined in:
lib/peruby/runtime/match_state.rb

Overview

Dynamic values exposed by Perl's most recent successful regexp match.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match_data = nil, subject = '') ⇒ MatchState

Returns a new instance of MatchState.



8
9
10
11
# File 'lib/peruby/runtime/match_state.rb', line 8

def initialize(match_data = nil, subject = '')
  @md = match_data
  @subject = subject
end

Instance Attribute Details

#mdObject (readonly)

Returns the value of attribute md.



6
7
8
# File 'lib/peruby/runtime/match_state.rb', line 6

def md
  @md
end

#subjectObject (readonly)

Returns the value of attribute subject.



6
7
8
# File 'lib/peruby/runtime/match_state.rb', line 6

def subject
  @subject
end

Instance Method Details

#fetch(sigil, name) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/peruby/runtime/match_state.rb', line 13

def fetch(sigil, name)
  return nil unless @md
  return scalar(name) if sigil == :scalar && name.match?(/\A(?:\d+|[&`'+])\z/)
  return offsets(name) if sigil == :array && %w[- +].include?(name)
  return named(name) if sigil == :hash && %w[- +].include?(name)

  nil
end