Created
October 20, 2015 16:41
-
-
Save yitsushi/d0e6f1582654b7f4e2f8 to your computer and use it in GitHub Desktop.
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
class TestConstMissing | |
# default value | |
DEFAULT_VALUE = 0 | |
# self because constants defined on that level | |
# and not in instances | |
def self.const_missing(name) | |
puts "[log] Nope! '#{name}'" | |
self::DEFAULT_VALUE | |
end | |
end | |
class MyStuff < TestConstMissing | |
# override default value | |
DEFAULT_VALUE = 1 | |
def do | |
puts "do '#{D}'" | |
end | |
end | |
stuff = MyStuff.new | |
stuff.do | |
puts "Outside: '#{MyStuff::D}'" | |
puts "Parent: '#{TestConstMissing::D}'" | |
# Output: | |
# ====== | |
# [log] Nope! 'D' | |
# do '1' | |
# [log] Nope! 'D' | |
# Outside: '1' | |
# [log] Nope! 'D' | |
# Parent: '0' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment