Last active
January 13, 2022 20:08
-
-
Save stevenharman/2968308 to your computer and use it in GitHub Desktop.
ProTip™ on Constant Scoping and Resolution in Ruby: One of these is not like the other.
This file contains 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
module Root | |
FOO = 'Ayyyy!' | |
end | |
module Root::NotNested | |
def self.speak | |
puts FOO | |
end | |
end | |
module Root | |
module Nested | |
def self.speak | |
puts FOO | |
end | |
end | |
end | |
Root::FOO #=> 'Ayyyy!' | |
Root::NotNested.speak #=> NameError: uninitialized constant Root::NotNested::FOO | |
Root::Nested.speak #=> 'Ayyyy!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:) For my personal reference:
http://stackoverflow.com/questions/6927566/using-instead-of-module-for-ruby-namespacing
http://stackoverflow.com/questions/15119724/ruby-lexical-scope-vs-inheritance