Skip to content

Instantly share code, notes, and snippets.

@postmodern
Last active March 14, 2022 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save postmodern/1c10b1d03e2a0c832e2ad585f39cfc0d to your computer and use it in GitHub Desktop.
Save postmodern/1c10b1d03e2a0c832e2ad585f39cfc0d to your computer and use it in GitHub Desktop.
Discovered a weird ruby module constant scoping issue today.
TYPES: {:scope=>Namespace}
self::TYPES: {:scope=>Namespace::Mixin}
#!/usr/bin/env ruby
module Namespace
TYPES = {scope: self}
module Mixin
TYPES = {scope: self}
end
module SubNamespace
include Mixin
def self.test
TYPES # I would assume TYPES would resolve to Mixin::TYPES since it's included
end
def self.workaround
self::TYPES # explicitly access TYPES included by Mixin
end
end
end
print "TYPES:\t\t"
p Namespace::SubNamespace.test
print "self::TYPES:\t"
p Namespace::SubNamespace.workaround
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment