Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/automation_helpers/extensions/string.rb
Overview
Additional useful methods to extend the String class with
Class Method Summary collapse
-
.alphabet_char(type = :upper) ⇒ String
Generates a single random letter from an array of the english alphabet Accepts an input to determine what case of alphabet you want.
Instance Method Summary collapse
-
#pascalize ⇒ String
Converts a regular string name into it’s pascalized format This can then be used to generate a ClassName.
-
#sanitize_whitespace ⇒ String
Sanitize and convert every individual whitespace character to a regular space character (Does not sanitize newlines).
-
#snake_case ⇒ String
Converts a regular string into a snake cased format Will sanitize out some characters (Designed for titles).
-
#to_bool ⇒ Boolean
Cast any string to true or false.
Class Method Details
.alphabet_char(type = :upper) ⇒ String
Generates a single random letter from an array of the english alphabet Accepts an input to determine what case of alphabet you want
9 10 11 12 13 14 15 16 |
# File 'lib/automation_helpers/extensions/string.rb', line 9 def self.alphabet_char(type = :upper) case type when :upper; then ('A'..'Z').to_a.sample when :lower; then ('a'..'z').to_a.sample when :both; then (('a'..'z').to_a + ('A'..'Z').to_a).sample else raise ArgumentError, 'Invalid character type. Must be :upper (default), :lower or :both' end end |
Instance Method Details
#pascalize ⇒ String
Converts a regular string name into it’s pascalized format This can then be used to generate a ClassName
22 23 24 |
# File 'lib/automation_helpers/extensions/string.rb', line 22 def pascalize split('_').map(&:capitalize).join end |
#sanitize_whitespace ⇒ String
Sanitize and convert every individual whitespace character to a regular space character (Does not sanitize newlines)
30 31 32 |
# File 'lib/automation_helpers/extensions/string.rb', line 30 def sanitize_whitespace gsub(/[ \t\r\f\u00A0]/, ' ') end |
#snake_case ⇒ String
Converts a regular string into a snake cased format Will sanitize out some characters (Designed for titles)
38 39 40 41 42 43 44 45 46 |
# File 'lib/automation_helpers/extensions/string.rb', line 38 def snake_case gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .squeeze(' ') .tr('-', '_') .tr(' ', '_') .tr("'", '') .downcase end |
#to_bool ⇒ Boolean
Cast any string to true or false
51 52 53 |
# File 'lib/automation_helpers/extensions/string.rb', line 51 def to_bool %w[yes on true].include?(downcase) end |