Class: Peruby::Op::Transliterate

Inherits:
Base
  • Object
show all
Defined in:
lib/peruby/op/regexp.rb

Overview

Perl tr/// and y/// character translation.

Instance Method Summary collapse

Methods inherited from Base

#argument_cells, #local_slot, #lvalue, #run_subroutine, #to_callable, #warning_directive?

Constructor Details

#initialize(quote) ⇒ Transliterate

Returns a new instance of Transliterate.



227
228
229
# File 'lib/peruby/op/regexp.rb', line 227

def initialize(quote)
  @quote = quote
end

Instance Method Details

#apply_to(cell, _env, context) ⇒ Object



237
238
239
240
241
242
243
244
245
# File 'lib/peruby/op/regexp.rb', line 237

def apply_to(cell, _env, context)
  source, replacement = @quote.parts
  subject = Conv.to_str(cell.get)
  search = @quote.modifiers.include?('c') ? "^#{source}" : source
  count = subject.count(search)
  result = translate(subject, search, replacement)
  value = @quote.modifiers.include?('r') ? result : (cell.set(result) && count)
  context == :list ? [Scalar.new(value)] : value
end

#lvalue_required?Boolean

Returns:

  • (Boolean)


235
# File 'lib/peruby/op/regexp.rb', line 235

def lvalue_required? = !@quote.modifiers.include?('r')

#run(env, context) ⇒ Object



231
232
233
# File 'lib/peruby/op/regexp.rb', line 231

def run(env, context)
  apply_to(env.fetch(:scalar, '_'), env, context)
end