Class: RKelly::CharRange

Inherits:
Object
  • Object
show all
Defined in:
lib/rkelly/char_range.rb

Overview

Represents a syntax element location in source code - where it begins and where it ends.

It’s a value object - it can’t be modified.

Constant Summary collapse

EMPTY =

A re-usable empty range

CharRange.new(CharPos::EMPTY, CharPos::EMPTY)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ CharRange

Returns a new instance of CharRange.



11
12
13
14
# File 'lib/rkelly/char_range.rb', line 11

def initialize(from, to)
  @from = from
  @to = to
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



9
10
11
# File 'lib/rkelly/char_range.rb', line 9

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



9
10
11
# File 'lib/rkelly/char_range.rb', line 9

def to
  @to
end

Instance Method Details

#next(string) ⇒ Object

Creates a new range that immediately follows this one and contains the given string.



18
19
20
# File 'lib/rkelly/char_range.rb', line 18

def next(string)
  CharRange.new(@to.next(string.slice(0, 1)), @to.next(string))
end

#to_sObject Also known as: inspect



22
23
24
# File 'lib/rkelly/char_range.rb', line 22

def to_s
  "<#{@from}...#{@to}>"
end