Created
February 20, 2018 15:46
-
-
Save jqr/e7eaa773c819fbf02d4ea07807582cb8 to your computer and use it in GitHub Desktop.
Namespaces and Nesting
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
# Show an issue with reopened namespaces having lookups done using nesting | |
# instead of namespaces. Then show a simple work-around. | |
# Define a constant in a module with namespaced classes. | |
module Something | |
CONSTANT = true | |
end | |
# Reopen Something add add a class using nesting. | |
module Something | |
class Nested | |
def self.constant? | |
CONSTANT rescue "error" | |
end | |
end | |
end | |
# Reopen Something add add a class using shortcut. | |
class Something::Shortcut | |
def self.constant? | |
CONSTANT rescue "error" | |
end | |
end | |
# Reopen Something add add a class using shortcut but include Something. | |
class Something::ShortcutWithInclude | |
include Something | |
def self.constant? | |
CONSTANT rescue "error" | |
end | |
end | |
puts "Nested .constant? #{Something::Nested.constant? }" # => true | |
puts "Shortcut .constant? #{Something::Shortcut.constant? }" # => error | |
puts "ShortcutWithInclude.constant? #{Something::ShortcutWithInclude.constant?}" # => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment