Sha256: c93dfaccd0b4d129df68188cc2da0b39122eda9707599e7f7fcc75a56e917dae

Contents?: true

Size: 1020 Bytes

Versions: 4

Compression:

Stored size: 1020 Bytes

Contents

# 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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
steamrb-0.1.3 lib/ext/string.rb
steamrb-0.1.2 lib/ext/string.rb
steamrb-0.1.1 lib/ext/string.rb
steamrb-0.1.0 lib/ext/string.rb