# Ghost constants :-)

**URL:** <https://metaruby.com/t/ghost-constants/393>\
**Category:** Ruby Chat\
**Created:** [November 25, 2015, 7:19pm UTC](https://metaruby.com/t/ghost-constants/393 "2015-11-25T19:19:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielpclark](https://metaruby.com/user_avatar/metaruby.com/danielpclark/32/823_2.png) [@danielpclark](https://metaruby.com/u/danielpclark)\
**Post date:** [November 25, 2015, 7:19pm UTC](https://metaruby.com/t/ghost-constants/393/1 "2015-11-25T19:19:27Z")

</div>

Looking at this bug report [https://bugs.ruby-lang.org/issues/11718](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.

```ruby
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 

```

---

<div class="post-metadata">

**Author:** ![ludovic](https://metaruby.com/user_avatar/metaruby.com/ludovic/32/183_2.png) [@ludovic](https://metaruby.com/u/ludovic)\
**Post date:** [December 4, 2015, 8:04am UTC](https://metaruby.com/t/ghost-constants/393/2 "2015-12-04T08:04:19Z")

</div>

Thanks. A bit tricky, but interesting !
