Class: Sourcify::Common::Parser::SourceCode

Inherits:
Struct
  • Object
show all
Defined in:
lib/sourcify/lib/sourcify/common/parser/source_code.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fileObject

Returns the value of attribute file

Returns:

  • (Object)

    the current value of file



4
5
6
# File 'lib/sourcify/lib/sourcify/common/parser/source_code.rb', line 4

def file
  @file
end

#lineObject

Returns the value of attribute line

Returns:

  • (Object)

    the current value of line



4
5
6
# File 'lib/sourcify/lib/sourcify/common/parser/source_code.rb', line 4

def line
  @line
end

Instance Method Details

#from_file_to_sObject



18
19
20
21
22
23
# File 'lib/sourcify/lib/sourcify/common/parser/source_code.rb', line 18

def from_file_to_s
  File.open(file, 'r') do |fh|
    fh.extend(File::Tail).forward(line)
    fh.readlines.join
  end
end

#from_irb_to_sObject



25
26
27
28
29
# File 'lib/sourcify/lib/sourcify/common/parser/source_code.rb', line 25

def from_irb_to_s
  # Really owe it to Florian Groß's solution @ http://rubyquiz.com/quiz38.html ...
  # anyway, note that we use *line.succ* instead of *line* here.
  IRB.CurrentContext.io.line(line.succ .. -1).join
end

#from_pry_to_sObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/sourcify/lib/sourcify/common/parser/source_code.rb', line 31

def from_pry_to_s
  # NOTE: HACK to reliably construct the lines required, as:
  # * Pry.line_buffer always fail to show the last entry
  # * Pry.history deliberately skips duplicated consecutive lines,
  #   yet it reliably shows the last
  [
    Pry.line_buffer,
    Pry.history.to_a[-1]
  ].flatten[line.succ .. -1] * ""
end

#to_sObject



10
11
12
13
14
15
16
# File 'lib/sourcify/lib/sourcify/common/parser/source_code.rb', line 10

def to_s
  case file
  when /\(irb\)/, /\(irb\#\d+\)/ then from_irb_to_s
  when /\(pry\)/ then from_pry_to_s
  else from_file_to_s
  end
end