Class: Peruby::Op::Substitute
Overview
Perl s/// substitution, destructive unless /r is present.
Instance Method Summary collapse
- #apply_to(cell, env, context) ⇒ Object
-
#initialize(quote) ⇒ Substitute
constructor
A new instance of Substitute.
- #lvalue_required? ⇒ Boolean
- #run(env, context) ⇒ Object
Methods inherited from Base
#argument_cells, #local_slot, #lvalue, #run_subroutine, #to_callable, #warning_directive?
Constructor Details
#initialize(quote) ⇒ Substitute
Returns a new instance of Substitute.
177 178 179 |
# File 'lib/peruby/op/regexp.rb', line 177 def initialize(quote) @quote = quote end |
Instance Method Details
#apply_to(cell, env, context) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/peruby/op/regexp.rb', line 187 def apply_to(cell, env, context) subject = Conv.to_str(cell.get) regexp = RegexpCompiler.compile(Interpolation.pattern(@quote, env), @quote.modifiers) count = 0 replace = @quote.modifiers.include?('g') ? :gsub : :sub result = subject.public_send(replace, regexp) do count += 1 env.runtime.match!(Regexp.last_match, subject) replacement(env) end value = if @quote.modifiers.include?('r') result else cell.set(result) count.zero? ? '' : count end context == :list ? [Scalar.new(value)] : value end |
#lvalue_required? ⇒ Boolean
185 |
# File 'lib/peruby/op/regexp.rb', line 185 def lvalue_required? = !@quote.modifiers.include?('r') |
#run(env, context) ⇒ Object
181 182 183 |
# File 'lib/peruby/op/regexp.rb', line 181 def run(env, context) apply_to(env.fetch(:scalar, '_'), env, context) end |