Class: Rope::LeafNode
- Extended by:
- Forwardable
- Defined in:
- lib/rope/leaf_node.rb
Overview
Specifies a leaf node that contains a basic string
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The underlying data in the tree.
Attributes inherited from BasicNode
Instance Method Summary collapse
-
#initialize(data) ⇒ LeafNode
constructor
Initializes a node that contains a basic string.
- #replace!(index, length, substr) ⇒ Object
- #subtree(from, length) ⇒ Object
Methods inherited from BasicNode
#+, #rebalance!, #segment, #to_primitive
Constructor Details
#initialize(data) ⇒ LeafNode
Initializes a node that contains a basic string
14 15 16 17 18 |
# File 'lib/rope/leaf_node.rb', line 14 def initialize(data) @data = data.freeze #Freezes the data to protect against aliasing errors @length = data.length @depth = 0 end |
Instance Attribute Details
#data ⇒ Object (readonly)
The underlying data in the tree
9 10 11 |
# File 'lib/rope/leaf_node.rb', line 9 def data @data end |
Instance Method Details
#replace!(index, length, substr) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rope/leaf_node.rb', line 28 def replace!(index, length, substr) left = if(index == 0) LeafNode.new(substr) else InteriorNode.new( LeafNode.new(@data.slice(0,index)), LeafNode.new(substr) ) end if((index + length) < @data.length) InteriorNode.new( left, LeafNode.new(@data.slice(index + length, @data.length - (index + length))) ) else left end end |
#subtree(from, length) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/rope/leaf_node.rb', line 20 def subtree(from, length) if length == @data.length self else self.class.new(@data.slice(from, length)) end end |