Sha256: bfb799158d54ba1c5c96ca5c71b802cf9225b569ced48fe836bdbff8598f1aa4

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

require 'gorillib/string/inflector'
class String

  # +constantize+ tries to find a declared constant with the name specified
  # in the string. It raises a NameError when the name is not in CamelCase
  # or is not initialized.  See Gorillib::Inflector.constantize
  #
  # Examples
  #   "Module".constantize  # => Module
  #   "Class".constantize   # => Class
  #   "blargle".constantize # => NameError: wrong constant name blargle
  def constantize
    Gorillib::Inflector.constantize(self)
  end unless method_defined?(:constantize)

  # +safe_constantize+ tries to find a declared constant with the name specified
  # in the string. It returns nil when the name is not in CamelCase
  # or is not initialized.  See Gorillib::Model::Inflector.safe_constantize
  #
  # Examples
  #   "Module".safe_constantize  # => Module
  #   "Class".safe_constantize   # => Class
  #   "blargle".safe_constantize # => nil
  def safe_constantize
    Gorillib::Inflector.safe_constantize(self)
  end unless method_defined?(:safe_constantize)

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gorillib-0.4.0pre lib/gorillib/string/constantize.rb
gorillib-0.4.1pre lib/gorillib/string/constantize.rb