Class: Proc

Inherits:
Object show all
Defined in:
lib/core/proc.rb

Direct Known Subclasses

DelayedProc

Instance Method Summary collapse

Instance Method Details

#code(filename = source_file) ⇒ Object

Get the code, based on the inspect’s filename and line number start



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/core/proc.rb', line 4

def code(filename=source_file)
  return @code if @code
  current_block_count = 1
  line_count = 0
  
  source(filename).each do |line|
    if line =~ /do(\W*?)$/
      current_block_count+=1
    elsif line =~ /end$/
      current_block_count-=1
      if current_block_count == 0
        return @code = source(filename)[0..(line_count-1)].join("\n")
      end        
    end
    line_count+=1
  end    
end

#proc_infoObject

Grab the source location from the inspect method



46
47
48
49
50
51
52
53
# File 'lib/core/proc.rb', line 46

def proc_info
  return @proc_info if @proc_info
  if md = /^#<Proc:0x[0-9A-Fa-f]+@(.+):(\d+)>$/.match(inspect)
    filename, line = md.captures
    @proc_info = File.expand_path(filename), line.to_i
  end
  @proc_info
end

#source(filename = source_file, start_line = source_line_number) ⇒ Object

Source



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/core/proc.rb', line 23

def source(filename = source_file, start_line = source_line_number)
  @source ||= if File.exist?(filename)
    begin
      open(filename).read.split("\n")[start_line .. -1]
    rescue
      nil
    end
  else
    raise StandardError.new("Cannot find the source file for #{self.inspect}")
  end
end

#source_fileObject

Grab the location of the proc by filename



36
37
38
# File 'lib/core/proc.rb', line 36

def source_file
  @source_file ||= proc_info[0]
end

#source_line_numberObject

Grab the line number in the source_file



41
42
43
# File 'lib/core/proc.rb', line 41

def source_line_number
  @source_line_number ||= proc_info[1]
end