Skip to content

Instantly share code, notes, and snippets.

@kusor
Created March 12, 2009 14:24
Show Gist options
  • Save kusor/78088 to your computer and use it in GitHub Desktop.
Save kusor/78088 to your computer and use it in GitHub Desktop.
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