Report abuse

1
2
3
4
5
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
31
32
33
34
35
36
37
38
39
40
41
42
43
TEST = "OUT"

class A; end

class StaticScope
  def list
    ary = []
    start = self
    while start
      ary << start.module
      start = start.parent
    end

    return ary
  end
end

class MetaClass
  def inspect
    "#<MetaClass:0x#{object_id.to_s(16)}>"
  end
end

module Mod
  TEST = "CONST"
  
  class << self
    p MethodContext.current.method.staticscope.list
    puts "#{TEST} (Inside the Mod metaclass)"
    
    def test(klass)
      p MethodContext.current.method.staticscope.list
      puts "#{TEST} (Inside Mod.test)"
      class << klass
        p MethodContext.current.method.staticscope.list
        puts "#{TEST} (Inside the metaclass in Mod.test)" # Should output "CONST", but gives "OUT"
      end
    end
  end
end

Mod.test(A)