Class: MemoryProfiler::Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/memory_profiler/helpers.rb

Instance Method Summary collapse

Constructor Details

#initializeHelpers

Returns a new instance of Helpers.



4
5
6
7
8
# File 'lib/memory_profiler/helpers.rb', line 4

def initialize
  @gem_guess_cache = Hash.new
  @location_cache = Hash.new { |h,k| h[k] = Hash.new.compare_by_identity }
  @class_name_cache = Hash.new.compare_by_identity
end

Instance Method Details

#guess_gem(path) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/memory_profiler/helpers.rb', line 10

def guess_gem(path)
  @gem_guess_cache[path] ||=
    if /(\/gems\/.*)*\/gems\/(?<gemname>[^\/]+)/ =~ path
      gemname
    elsif /\/rubygems[\.\/]/ =~ path
      "rubygems".freeze
    elsif /(?<app>[^\/]+\/(bin|app|lib))/ =~ path
      app
    else
      "other".freeze
    end
end

#lookup_class_name(klass) ⇒ Object



27
28
29
# File 'lib/memory_profiler/helpers.rb', line 27

def lookup_class_name(klass)
  @class_name_cache[klass] ||= (klass && klass.name) || '<<Unknown>>'
end

#lookup_location(file, line) ⇒ Object



23
24
25
# File 'lib/memory_profiler/helpers.rb', line 23

def lookup_location(file, line)
  @location_cache[file][line] ||= "#{file}:#{line}"
end