Class: PutsDebuggerer::SourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/puts_debuggerer/source_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ SourceFile

Returns a new instance of SourceFile.



3
4
5
# File 'lib/puts_debuggerer/source_file.rb', line 3

def initialize(file_path)
  @file = File.new(file_path) if file_path && File.exist?(file_path)
end

Instance Method Details

#source(source_line_count, source_line_number) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/puts_debuggerer/source_file.rb', line 7

def source(source_line_count, source_line_number)
  @source = ''
  return @source if RUBY_ENGINE == 'opal'
  # For Opal Ruby compatibility, skip source lines if file does not respond to readline (as in Opal)
  lines = source_lines(source_line_count, source_line_number)
  @source = lines.join(' '*5) if @file.respond_to?(:readline)
  @source
end

#source_lines(source_line_count, source_line_number) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/puts_debuggerer/source_file.rb', line 16

def source_lines(source_line_count, source_line_number)
  lines = []
  begin
    while @file && @file.lineno < source_line_number + source_line_count
      file_line_number = @file.lineno + 1
      file_line = @file.readline
      if file_line_number >= source_line_number && file_line_number < source_line_number + source_line_count
        lines << file_line
      end
    end
  rescue EOFError
    # Done
  end
  lines
end