Skip to content

Instantly share code, notes, and snippets.

@jqr
Created February 20, 2018 15:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jqr/e7eaa773c819fbf02d4ea07807582cb8 to your computer and use it in GitHub Desktop.
Save jqr/e7eaa773c819fbf02d4ea07807582cb8 to your computer and use it in GitHub Desktop.
Namespaces and Nesting
# 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