Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/cliutils/ext/string_extensions.rb
Overview
String Class extensions
Instance Method Summary collapse
-
#blue ⇒ void
Makes the associated string blue.
-
#camelize ⇒ String
Converts a snake_case string to its CamelCase variant.
-
#colorize(color_code) ⇒ void
Outputs a string in a formatted color.
-
#cyan ⇒ void
Makes the associated string cyan.
-
#green ⇒ void
Makes the associated string green.
-
#purple ⇒ void
Makes the associated string purple.
-
#red ⇒ void
Makes the associated string red.
-
#snakify ⇒ String
Converts a CamelCase string to its snake_case variant.
-
#truncate(length = 30) ⇒ String
Truncates a string to a certain length and adds an ellipsis after.
-
#white ⇒ void
Makes the associated string white.
-
#yellow ⇒ void
Makes the associated string yellow.
Instance Method Details
#blue ⇒ void
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 |
#camelize ⇒ String
Converts a snake_case string to its CamelCase variant.
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.
20 21 22 |
# File 'lib/cliutils/ext/string_extensions.rb', line 20 def colorize(color_code) "\033[#{ color_code }m#{ self }\033[0m" end |
#cyan ⇒ void
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 |
#green ⇒ void
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 |
#purple ⇒ void
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 |
#red ⇒ void
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 |
#snakify ⇒ String
Converts a CamelCase string to its snake_case variant.
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.
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 |
#white ⇒ void
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 |
#yellow ⇒ void
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 |