Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/cliutils/ext/string_extensions.rb

Overview

String Class extensions

Instance Method Summary collapse

Instance Method Details

#bluevoid

This method returns an undefined value.

Makes the associated string blue.



5
6
7
# File 'lib/cliutils/ext/string_extensions.rb', line 5

def blue
  colorize(34)
end

#camelizeString

Converts a snake_case string to its CamelCase variant.

Returns:



12
13
14
15
# File 'lib/cliutils/ext/string_extensions.rb', line 12

def camelize
  return self if self !~ /_/ && self =~ /[A-Z]+.*/
  split('_').map{|e| e.capitalize}.join
end

#colorize(color_code) ⇒ void

This method returns an undefined value.

Outputs a string in a formatted color.

Parameters:

  • The code to use



20
21
22
# File 'lib/cliutils/ext/string_extensions.rb', line 20

def colorize(color_code)
  "\033[#{ color_code }m#{ self }\033[0m"
end

#cyanvoid

This method returns an undefined value.

Makes the associated string cyan.



26
27
28
# File 'lib/cliutils/ext/string_extensions.rb', line 26

def cyan
  colorize(36)
end

#greenvoid

This method returns an undefined value.

Makes the associated string green.



32
33
34
# File 'lib/cliutils/ext/string_extensions.rb', line 32

def green
  colorize(32)
end

#purplevoid

This method returns an undefined value.

Makes the associated string purple.



38
39
40
# File 'lib/cliutils/ext/string_extensions.rb', line 38

def purple
  colorize(35)
end

#redvoid

This method returns an undefined value.

Makes the associated string red.



44
45
46
# File 'lib/cliutils/ext/string_extensions.rb', line 44

def red
  colorize(31)
end

#snakifyString

Converts a CamelCase string to its snake_case variant.

Returns:



51
52
53
54
55
56
# File 'lib/cliutils/ext/string_extensions.rb', line 51

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

#truncate(length = 30) ⇒ String

Truncates a string to a certain length and adds an ellipsis after.

Parameters:

  • (defaults to: 30)

    The length to truncate at

Returns:



62
63
64
65
# File 'lib/cliutils/ext/string_extensions.rb', line 62

def truncate(length = 30)
  return self if self.length < length
  self[0..length].gsub(/\s\w+\s*$/, '...')
end

#whitevoid

This method returns an undefined value.

Makes the associated string white.



69
70
71
# File 'lib/cliutils/ext/string_extensions.rb', line 69

def white
  colorize(37)
end

#yellowvoid

This method returns an undefined value.

Makes the associated string yellow.



75
76
77
# File 'lib/cliutils/ext/string_extensions.rb', line 75

def yellow
  colorize(33)
end