Sha256: 35e471dac74e7ffbbb0a9670b7653ebabe2428c5567e531762fe979149b49615

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

#  String Class extensions
class String
  # Makes the associated string blue.
  # @return [void]
  def blue
    colorize(34)
  end

  # Converts a snake_case string to its
  # CamelCase variant.
  # @return [String]
  def camelize
    return self if self !~ /_/ && self =~ /[A-Z]+.*/
    split('_').map{|e| e.capitalize}.join
  end

  # Outputs a string in a formatted color.
  # @param [<Integer, String>] color_code The code to use
  # @return [void]
  def colorize(color_code)
    "\033[#{ color_code }m#{ self }\033[0m"
  end

  # Makes the associated string cyan.
  # @return [void]
  def cyan
    colorize(36)
  end

  # Makes the associated string green.
  # @return [void]
  def green
    colorize(32)
  end

  # Makes the associated string purple.
  # @return [void]
  def purple
    colorize(35)
  end

  # Makes the associated string red.
  # @return [void]
  def red
    colorize(31)
  end

  # Converts a CamelCase string to its
  # snake_case variant.
  # @return [String]
  def snakify
    return downcase if match(/\A[A-Z]+\z/)
    gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
    gsub(/([a-z])([A-Z])/, '\1_\2').
    downcase
  end

  # Truncates a string to a certain length
  # and adds an ellipsis after.
  # @param [Fixnum] length The length to truncate at
  # @return [String]
  def truncate(length = 30)
    self[0..length].gsub(/\s\w+\s*$/, '...')
  end

  # Makes the associated string white.
  # @return [void]
  def white
    colorize(37)
  end

  # Makes the associated string yellow.
  # @return [void]
  def yellow
    colorize(33)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cliutils-2.2.5 lib/cliutils/ext/string_extensions.rb