Created
March 12, 2009 14:24
-
-
Save kusor/78088 to your computer and use it in GitHub Desktop.
This file contains 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
require 'rubygems' | |
require 'extlib' | |
module Extlib | |
module Inflection | |
class << self | |
# Take a complete module/class name and returns the scope where that module/class has been defined: | |
# | |
# @example | |
# module_scope("ActiveRecord::CoreExtensions::String::Inflections") #=> "ActiveRecord::CoreExtensions::String" | |
# module_scope("::Inflections") #=> "Kernel" | |
# module_scope("Inflections") #=> "Kernel" | |
# | |
# You can use the return string with Extlib::Inflection.constantize() in order to reopen that module/class. | |
# | |
def module_scope(str) | |
new_str = str.gsub(/::#{Extlib::Inflection.demodulize(str)}/,'') | |
(new_str == str || new_str.empty?) ? "Kernel" : new_str | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment