Skip to content

Instantly share code, notes, and snippets.

@dblack
Created June 19, 2010 20:09
Show Gist options
  • Save dblack/445232 to your computer and use it in GitHub Desktop.
Save dblack/445232 to your computer and use it in GitHub Desktop.
# An example of inject: deep_const_get.
# This versions of const_get works with nested constants
# (unlike regular const_get, which chokes on "::").
#
# This implementation does some output along the way to
# show you what's happening.
class Module
def deep_const_get(const)
segments = const.split("::")
segments.inject(self) do |acc, current|
puts "About to resolve #{current} on #{acc}"
acc.const_get(current)
end
end
end
class A
module B
class C
module D
end
end
end
end
result = A.deep_const_get("B::C::D")
puts "Got #{result}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment