Class: Sourcify::Proc::Parser::SourceCode

Inherits:
Struct
  • Object
show all
Defined in:
lib/sourcify/proc/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/proc/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/proc/parser/source_code.rb', line 4

def line
  @line
end

Instance Method Details

#from_file_to_sObject



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

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



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

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

#to_sObject



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

def to_s
  case file
  when /\(irb\)/ then from_irb_to_s
  else from_file_to_s
  end
end