Created
April 30, 2013 18:27
-
-
Save anonymous/5490823 to your computer and use it in GitHub Desktop.
Recursive Constant Getting -- sans-ActiveSupport
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
Module#recursive_const_get | |
class Module | |
def recursive_const_get(name) | |
name.to_s.split("::").inject(self) do |b, c| | |
b.const_get(c) | |
end | |
end | |
end | |
#Example | |
module Foo | |
BAR = "Hello World!" | |
end | |
Module.const_get("Foo::BAR") rescue $! | |
# => #<NameError: wrong constant name Foo::BAR> | |
Module.const_get("Foo").const_get("BAR") | |
# => "Hello World!" | |
Module.recursive_const_get("Foo::BAR") | |
# => "Hello World!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment