# frozen_string_literal: true # Core string extensions class String # Camelize a string # # @example Camelize a string # "hello_world".camelize # => HelloWorld def camelize split('_').collect(&:capitalize).join end alias classify camelize # Underscore a String. This method will also downcase the string # # @example Underscore a string # "HelloWorld" # => "hello_world" def underscore gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .downcase end # Look up a constant via a string. # # @example Using constantize # "Object".constantize # => Object # rubocop:disable LineLength def constantize names = split('::') names.shift if names.empty? || names.first.empty? constant = Object names.each do |name| constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name) end constant end alias snakecase underscore end