Created
April 7, 2009 21:26
-
-
Save burke/91468 to your computer and use it in GitHub Desktop.
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
$LOAD_PATH << File.join(File.dirname(__FILE__),"lib") | |
class String | |
# Convert a CamelCase string (class name) into an underscorized | |
# not_camel_case string. | |
def underscorize | |
self.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). | |
gsub(/([a-z\d])([A-Z])/,'\1_\2'). | |
downcase | |
end | |
end | |
# When a new ClassName is used check ./lib for that class_name. If it exists, | |
# require it and return it. If not, raise NameError like would happen anyway. | |
def Object.const_missing(klass) | |
file = klass.to_s.underscorize | |
begin | |
require file | |
return Object.const_get(klass) | |
rescue LoadError | |
raise NameError, "uninitialized constant #{klass}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment