Class: Slimi::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/slimi/range.rb

Overview

Get line-based information from source code and its index.

Instance Method Summary collapse

Constructor Details

#initialize(index:, source:) ⇒ Range

Returns a new instance of Range.

Parameters:

  • index (Integer)

    0-indexed per-character index.

  • source (String)


8
9
10
11
# File 'lib/slimi/range.rb', line 8

def initialize(index:, source:)
  @index = index
  @source = source
end

Instance Method Details

#columnInteger

Returns 1-indexed column index.

Returns:

  • (Integer)

    1-indexed column index.



14
15
16
# File 'lib/slimi/range.rb', line 14

def column
  (@index - line_beginning_index) + 1
end

#lineObject



18
19
20
# File 'lib/slimi/range.rb', line 18

def line
  @source[line_beginning_index...line_ending_index]
end

#line_numberInteger

Returns 1-indexed line index.

Returns:

  • (Integer)

    1-indexed line index.



23
24
25
# File 'lib/slimi/range.rb', line 23

def line_number
  @source[0..@index].scan(/^/).length
end