Class: Peruby::Op::Match
- Inherits:
-
Base
- Object
- Base
- Peruby::Op::Match
show all
- Defined in:
- lib/peruby/op/regexp.rb
Overview
Regexp match, using $_ when no explicit binding subject exists.
Instance Method Summary
collapse
Methods inherited from Base
#argument_cells, #local_slot, #lvalue, #run_subroutine, #to_callable, #warning_directive?
Constructor Details
#initialize(quote) ⇒ Match
Returns a new instance of Match.
7
8
9
10
11
12
13
|
# File 'lib/peruby/op/regexp.rb', line 7
def initialize(quote)
@quote = quote
return unless quote
source = quote.parts.first
@regexp = RegexpCompiler.compile(source, quote.modifiers) unless source.match?(Interpolation.variable_pattern)
end
|
Instance Method Details
#match_callable ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/peruby/op/regexp.rb', line 32
def match_callable
return if !@regexp || @quote.modifiers.include?('g')
regexp = @regexp
lambda do |value, env, context|
subject = value
subject = Conv.to_str(subject) unless subject.is_a?(String)
match = regexp.match(subject)
env.runtime.match!(match, subject) if match
next match ? 1 : '' unless context == :list
values = if match
match.captures.empty? ? [1] : match.captures
else
[]
end
values.map { |value| Scalar.new(value) }
end
end
|
#match_on(cell, env, context) ⇒ Object
19
20
21
22
|
# File 'lib/peruby/op/regexp.rb', line 19
def match_on(cell, env, context)
regexp = @regexp || RegexpCompiler.compile(Interpolation.pattern(@quote, env), @quote.modifiers)
match_regexp(regexp, cell, env, context, global: @quote.modifiers.include?('g'))
end
|
#match_regexp(regexp, cell, env, context, global: false) ⇒ Object
24
25
26
27
28
29
30
|
# File 'lib/peruby/op/regexp.rb', line 24
def match_regexp(regexp, cell, env, context, global: false)
return global(regexp, cell, env, context) if global
match = regexp.match(Conv.to_str(cell.get))
env.runtime.match!(match, Conv.to_str(cell.get)) if match
result(match, context)
end
|
#run(env, context) ⇒ Object
15
16
17
|
# File 'lib/peruby/op/regexp.rb', line 15
def run(env, context)
match_on(env.fetch(:scalar, '_'), env, context)
end
|