Skip to content

Instantly share code, notes, and snippets.

@dmikurube
Last active March 16, 2017 12:10
Show Gist options
  • Save dmikurube/d0d238f0bccdb4120768a083f2813329 to your computer and use it in GitHub Desktop.
Save dmikurube/d0d238f0bccdb4120768a083f2813329 to your computer and use it in GitHub Desktop.
Hook CONSTANT-like in Ruby
module M
REAL_CONST = 'foo'
def self.const_missing(name)
if name == :MISSING_DUMMY
'qux'
else
super
end
end
def self.dummy_non_const
'bar'
end
def self.DUMMY_CONST
'baz'
end
end
puts M::REAL_CONST
puts M::MISSING_DUMMY
puts M::dummy_non_const
puts M::DUMMY_CONST
@dmikurube
Copy link
Author

dmikurube commented Mar 16, 2017

foo
qux
bar
const_hook.rb:8:in `const_missing': uninitialized constant M::DUMMY_CONST (NameError)
	from const_hook.rb:24:in `<main>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment