Skip to content

Instantly share code, notes, and snippets.

@burke
Created April 7, 2009 21:26
Show Gist options
  • Save burke/91468 to your computer and use it in GitHub Desktop.
Save burke/91468 to your computer and use it in GitHub Desktop.
$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