Class: Rope::BasicRope
- Inherits:
-
Object
- Object
- Rope::BasicRope
- Extended by:
- Forwardable
- Defined in:
- lib/rope/basic_rope.rb
Overview
BasicRope is a data-type-agnostic Rope data structure. The data type is typically a string but can be anything that implements the following methods:
-
length - integer length of a unit of the data type
-
slice - break a unit of the data type into two pieces on an index boundary
-
+ - join two pieces of the data type together
-
alias for slice
-
Class Method Summary collapse
Instance Method Summary collapse
-
#+(other) ⇒ Object
Concatenates this rope with another rope or string.
- #<<(rhs) ⇒ Object
-
#==(other) ⇒ Object
Tests whether this rope is equal to another rope.
- #[]=(index, length = 1, rhs) ⇒ Object
-
#dup ⇒ Object
Creates a copy of this rope.
-
#initialize(arg = nil) ⇒ BasicRope
constructor
Initializes a new rope.
-
#slice(*args) ⇒ Object
(also: #[])
Gets a slice of this rope.
Constructor Details
#initialize(arg = nil) ⇒ BasicRope
Initializes a new rope
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rope/basic_rope.rb', line 28 def initialize(arg=nil) case arg when BasicNode @root = arg when NilClass @root = LeafNode.new(primitive_type.new) when primitive_type @root = LeafNode.new(arg) when self.class, InteriorNode @root = LeafNode.new(arg.to_primitive) else raise ArgumentError, "#{arg} is not a #{primitive_type}" end end |
Class Method Details
.rope_for_type(type, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/rope/basic_rope.rb', line 17 def self.rope_for_type(type, &block) Class.new(self) do define_method :primitive_type do type end class_eval &block if block_given? end end |
Instance Method Details
#+(other) ⇒ Object
Concatenates this rope with another rope or string
46 47 48 |
# File 'lib/rope/basic_rope.rb', line 46 def +(other) self.class.new(concatenate(other)) end |
#<<(rhs) ⇒ Object
82 83 84 |
# File 'lib/rope/basic_rope.rb', line 82 def <<(rhs) self[length] = rhs end |
#==(other) ⇒ Object
Tests whether this rope is equal to another rope
51 52 53 |
# File 'lib/rope/basic_rope.rb', line 51 def ==(other) to_primitive == (BasicRope === other ? other.to_primitive : other) end |
#[]=(index, length = 1, rhs) ⇒ Object
76 77 78 79 |
# File 'lib/rope/basic_rope.rb', line 76 def []=(index, length=1, rhs) @root = @root.replace!(index, length, rhs) self end |
#dup ⇒ Object
Creates a copy of this rope
56 57 58 59 |
# File 'lib/rope/basic_rope.rb', line 56 def dup root.freeze #Prevents errors when self.class.new(root) end |
#slice(*args) ⇒ Object Also known as: []
Gets a slice of this rope
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rope/basic_rope.rb', line 62 def slice(*args) slice = root.slice(*args) case slice when Fixnum # slice(Fixnum) returns a plain Fixnum slice when BasicNode, primitive_type # create a new Rope with the returned tree as the root self.class.new(slice) else nil end end |