Module: Devel::Which::ForObject

Defined in:
lib/devel/which/forobject.rb

Class Method Summary collapse

Class Method Details

.whereis_library(lib, opt = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/devel/which/forobject.rb', line 33

def whereis_library(lib, opt = {})
  unless lib.is_a? String
    raise TypeError,
      "wrong argument type #{lib.type} (expected String)"
  end
  unless opt.is_a? Hash
    raise TypeError,
      "wrong argument type #{lib.type} (expected Hash)"
  end
  optpath = opt[:path] || opt[:p] || []
  optext = opt[:ext] || opt[:e] || ""
  ext = ["rb", RbConfig::CONFIG["DLEXT"], RbConfig::CONFIG["DLEXT2"], optext]
  ext.map!{|i| i.length > 0 ? ".#{i}" : nil}
  ext.compact!
  ext.push("")

  at = []

  ($: + optpath).each{|path|
    with = nil
    file = "#{File::expand_path(path)}/#{lib}"

    begin
      ext.each{|with|
        if (test(?f, file+with) && test(?r, file+with) rescue false)
          at << "#{path}/#{lib}#{with}"
        end
      }
    rescue
      next
    end
  }

  at
end

.which_library(lib) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/devel/which/forobject.rb', line 6

def which_library(lib)
  unless lib.is_a? String
    raise TypeError,
      "wrong argument type #{lib.type} (expected String)"
  end
  ext = ["rb", RbConfig::CONFIG["DLEXT"], RbConfig::CONFIG["DLEXT2"]]
  ext.map!{|i| i.length > 0 ? ".#{i}" : nil}
  ext.compact!
  ext.push("")

  at = with = nil
  at = $:.find{|path|
    file = "#{File::expand_path(path)}/#{lib}"

    begin
      with = ext.find{|i|
        test(?f, file+i) && test(?r, file+i)
      }
    rescue
      next
    end
  }

  "#{at}/#{lib}#{with}" if at
end