Class: RKelly::CharPos
- Inherits:
-
Object
- Object
- RKelly::CharPos
- Defined in:
- lib/rkelly/char_pos.rb
Overview
Represents a character position in source code.
It's a value object - it can't be modified.
Constant Summary collapse
- EMPTY =
A re-usable empty position
CharPos.new(1,0,-1)
Instance Attribute Summary collapse
-
#char ⇒ Object
readonly
Returns the value of attribute char.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
Instance Method Summary collapse
-
#initialize(line, char, index) ⇒ CharPos
constructor
A new instance of CharPos.
-
#next(string) ⇒ Object
Creates a new character position that's a given string away from this one.
- #to_s ⇒ Object (also: #inspect)
Constructor Details
#initialize(line, char, index) ⇒ CharPos
Returns a new instance of CharPos.
9 10 11 12 13 |
# File 'lib/rkelly/char_pos.rb', line 9 def initialize(line, char, index) @line = line @char = char @index = index end |
Instance Attribute Details
#char ⇒ Object (readonly)
Returns the value of attribute char.
7 8 9 |
# File 'lib/rkelly/char_pos.rb', line 7 def char @char end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
7 8 9 |
# File 'lib/rkelly/char_pos.rb', line 7 def index @index end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
7 8 9 |
# File 'lib/rkelly/char_pos.rb', line 7 def line @line end |
Instance Method Details
#next(string) ⇒ Object
Creates a new character position that's a given string away from this one.
17 18 19 20 21 22 23 24 |
# File 'lib/rkelly/char_pos.rb', line 17 def next(string) if string.include?("\n") lines = string.split(/\n/, -1) CharPos.new(@line + lines.length - 1, lines.last.length, @index + string.length) else CharPos.new(@line, @char + string.length, @index + string.length) end end |
#to_s ⇒ Object Also known as: inspect
26 27 28 |
# File 'lib/rkelly/char_pos.rb', line 26 def to_s "{line:#{@line} char:#{@char} (#{@index})}" end |