Module: Devel::Which::ForModule

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

Class Method Summary collapse

Class Method Details

.which_constant(mod, name) ⇒ Object



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

def which_constant(mod, name)

  name  = Devel::Which::name2string(name)
  nest  = []
  pos   = 0
  space = mod.name

  begin
    pos -= 1
    space = space[0..pos]
    nest.push(space)
  end while pos = space.rindex(/::/)

  nest.map!{|i| eval "::#{i}"}
  mod.ancestors.each{|i| nest.push i}
  a = (class << self; self; end).ancestors.each{|i| nest.push i}
  cand = nest.find_all{|m| m.const_defined? name}
  last = cand.shift
  return nil if not last
  value = last.const_get(name)

  while car = cand.shift
    break if value != car.const_get(name)
    last = car
  end

  last
end

.which_method(mod, name) ⇒ Object



37
38
39
40
41
42
# File 'lib/devel/which/formodule.rb', line 37

def which_method(mod, name)
  name = Devel::Which::name2string(name)
  mod.ancestors.find{|a|
    a.instance_methods.include?(name)
  }
end