Class: Peruby::Op::Builtin::SubstringCell

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

Overview

Mutable view used when substr appears on the left of an assignment.

Instance Method Summary collapse

Constructor Details

#initialize(cell, offset, length) ⇒ SubstringCell

Returns a new instance of SubstringCell.



730
731
732
733
734
# File 'lib/peruby/op/builtin.rb', line 730

def initialize(cell, offset, length)
  @cell = cell
  @offset = offset
  @length = length
end

Instance Method Details

#getObject



736
737
738
739
# File 'lib/peruby/op/builtin.rb', line 736

def get
  string = Conv.to_str(@cell.get)
  @length ? string.slice(@offset, @length) : string.slice(@offset..)
end

#set(value) ⇒ Object



741
742
743
744
745
746
# File 'lib/peruby/op/builtin.rb', line 741

def set(value)
  string = Conv.to_str(@cell.get).dup
  @length ? string[@offset, @length] = Conv.to_str(value) : string[@offset..] = Conv.to_str(value)
  @cell.set(string)
  self
end