Ghost constants :-)

Looking at this bug report https://bugs.ruby-lang.org/issues/11718 Ruby has an intentional bug. Calling a constant on nil just follows standard constant lookup. So if you define a constant as nil it will safely pass further constants up the lookup chain.

module A
  class B
    def test
      "hello"
    end
  end
end

include A
defined? B
# => "constant" 

A = nil
#(irb):10: warning: already initialized constant A
#(irb):1: warning: previous definition of A was here
# => nil 

A::B.new.test
# => "hello" 

A
# => nil 
2 Likes

Thanks. A bit tricky, but interesting !

1 Like