Class: Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/helpers.rb

Class Method Summary collapse

Class Method Details

.getLastCharacter(string) ⇒ String

Used to retrieve the last character of a string

Parameters:

  • string (String)

    The string that is used to retrieve the last character

Returns:

  • (String)

    The last character of the string

Raises:

  • (TypeError)

    if the string is of the wrong type



7
8
9
10
# File 'lib/helpers/helpers.rb', line 7

def self.getLastCharacter(string)
    raise TypeError unless string.is_a? String
    return string[-1]
end

.removeLastCharacterIf(string, character) ⇒ String

Remove the last character of a string if the character is a given definition

Parameters:

  • string (String)

    The string to remove a character from

  • character (String)

    The character to remove from the string

Returns:

  • (String)

    The newly edited string with the removed character

Raises:

  • (TypeError)

    if the character is of the wrong type



17
18
19
20
21
22
# File 'lib/helpers/helpers.rb', line 17

def self.removeLastCharacterIf(string, character)
    raise TypeError unless character.is_a? String

    string = string.chomp(character)
    return string
end

.removeMultipleLastCharacterIf(string, characters) ⇒ String

Remove the multiple last character of a string if the character is a given definition

Parameters:

  • string (String)

    The string to remove a character from

  • characters (String)

    The character to remove from the string

Returns:

  • (String)

    The newly edited string with the removed character

Raises:

  • (TypeError)

    if the character is of the wrong type



29
30
31
32
33
34
35
36
37
# File 'lib/helpers/helpers.rb', line 29

def self.removeMultipleLastCharacterIf(string, characters)
    raise TypeError unless characters.is_a? Array

    characters.each { |i|
        string = removeLastCharacterIf(string, i)
    }

    return string
end

.replaceAllCharacter(string, pattern, replacement) ⇒ String

Replace all characters in a string given a pattern and a replacement string

Parameters:

  • string (String)

    The string to remove a character from

  • pattern (String)

    The pattern to be taken into account when analyzing the string

  • replacement (String)

    The replacement string to use when the pattern is found

Returns:

  • (String)

    The newly edited string with the replacement and pattern applied to it

Raises:

  • (TypeError)

    if the string, pattern or the replacement is of the wrong type



45
46
47
48
49
50
51
52
53
# File 'lib/helpers/helpers.rb', line 45

def self.replaceAllCharacter(string, pattern, replacement)
    raise TypeError unless string.is_a? String
    raise TypeError unless pattern.is_a? String
    raise TypeError unless replacement.is_a? String

    string = string.gsub(pattern, replacement)
    
    return string
end