Last active
March 14, 2022 09:00
-
-
Save postmodern/1c10b1d03e2a0c832e2ad585f39cfc0d to your computer and use it in GitHub Desktop.
Discovered a weird ruby module constant scoping issue today.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TYPES: {:scope=>Namespace} | |
self::TYPES: {:scope=>Namespace::Mixin} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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