Class: Pry::Code::CodeRange Private

Inherits:
Object show all
Defined in:
lib/pry/code/code_range.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents a range of lines in a code listing.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_line, end_line = nil) ⇒ CodeRange

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of CodeRange

Parameters:

  • start_line (Integer)
  • end_line (Integer?) (defaults to: nil)

11
12
13
14
15
# File 'lib/pry/code/code_range.rb', line 11

def initialize(start_line, end_line = nil)
  @start_line = start_line
  @end_line   = end_line
  force_set_end_line
end

Instance Attribute Details

#end_lineObject (readonly, private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


27
28
29
# File 'lib/pry/code/code_range.rb', line 27

def end_line
  @end_line
end

#start_lineObject (readonly, private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


25
26
27
# File 'lib/pry/code/code_range.rb', line 25

def start_line
  @start_line
end

Instance Method Details

#find_end_index(lines) ⇒ Integer (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Integer)

57
58
59
60
61
# File 'lib/pry/code/code_range.rb', line 57

def find_end_index(lines)
  return end_line if end_line < 0

  (lines.index { |loc| loc.lineno > end_line } || 0) - 1
end

#find_start_index(lines) ⇒ Integer (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Integer)

50
51
52
53
54
# File 'lib/pry/code/code_range.rb', line 50

def find_start_index(lines)
  return start_line if start_line < 0

  lines.index { |loc| loc.lineno >= start_line } || lines.length
end

#force_set_end_line (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

If end_line is equal to nil, then calculate it from the first parameter, start_line. Otherwise, leave it as it is.


32
33
34
35
36
37
38
# File 'lib/pry/code/code_range.rb', line 32

def force_set_end_line
  if start_line.is_a?(Range)
    set_end_line_from_range
  else
    @end_line ||= start_line
  end
end

#indices(lines) ⇒ Array<Integer> (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds indices of start_line and end_line in the given Array of +lines+.

Parameters:

  • lines (Array<LOC>)

Returns:

  • (Array<Integer>)

45
46
47
# File 'lib/pry/code/code_range.rb', line 45

def indices(lines)
  [find_start_index(lines), find_end_index(lines)]
end

#indices_range(lines) ⇒ Range

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • lines (Array<LOC>)

Returns:

  • (Range)

19
20
21
# File 'lib/pry/code/code_range.rb', line 19

def indices_range(lines)
  Range.new(*indices(lines))
end

#set_end_line_from_range (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

For example, if the range is 4..10, then start_line would be equal to 4 and end_line to 10.


66
67
68
69
70
# File 'lib/pry/code/code_range.rb', line 66

def set_end_line_from_range
  @end_line = start_line.last
  @end_line -= 1 if start_line.exclude_end?
  @start_line = start_line.first
end