Skip to content

Instantly share code, notes, and snippets.

@yitsushi
Created October 20, 2015 16:41
Show Gist options
  • Save yitsushi/d0e6f1582654b7f4e2f8 to your computer and use it in GitHub Desktop.
Save yitsushi/d0e6f1582654b7f4e2f8 to your computer and use it in GitHub Desktop.
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