Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/automation_helpers/extensions/string.rb

Overview

Additional useful methods to extend the String class with

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:



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

#pascalizeString

Converts a regular string name into it’s pascalized format This can then be used to generate a ClassName

Returns:



22
23
24
# File 'lib/automation_helpers/extensions/string.rb', line 22

def pascalize
  split('_').map(&:capitalize).join
end

#sanitize_whitespaceString

Sanitize and convert every individual whitespace character to a regular space character (Does not sanitize newlines)

Returns:



30
31
32
# File 'lib/automation_helpers/extensions/string.rb', line 30

def sanitize_whitespace
  gsub(/[ \t\r\f\u00A0]/, ' ')
end

#snake_caseString

Converts a regular string into a snake cased format Will sanitize out some characters (Designed for titles)

Returns:



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_boolBoolean

Cast any string to true or false

Returns:

  • (Boolean)


51
52
53
# File 'lib/automation_helpers/extensions/string.rb', line 51

def to_bool
  %w[yes on true].include?(downcase)
end