Class: Systemdy::Utility::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/systemdy/utility/formatter.rb

Overview

Allows to formatting provided data into another type

Class Method Summary collapse

Class Method Details

.remove_newline_from_system_command(system_call) ⇒ String

TODO:

execute system calls with backtick instead of system() beacuse system() return only true or false and not the expected value

method for remove \n characters from system calls

Examples:

remove \n characters from system calls

command_without_new_line = Systemdy::Utility::Formatter.remove_newline_from_system_command(`ls -la`)

Parameters:

  • system_call (String)

    system call to remove \n characters from

Returns:

  • (String)

    the result of a system call without \n characters



40
41
42
# File 'lib/systemdy/utility/formatter.rb', line 40

def self.remove_newline_from_system_command(system_call)
    system_call.chomp!    # remove \n from system call returned value
end

.return_an_array_from(string_to_parse, argument_splitter: '\n', remove_blank_elements: true) ⇒ Array

method for convert a string into an array without blank elements

Examples:

convert a complex string into an array

list_of_info = Systemdy::Utility::Formatter.return_an_array_from("5432/tcp  info", argument_splitter: '/', remove_blank_elements: false) #=> ["5432", "tcp", "", "", "info"]

convert a complex string into an array without blank elements

list_of_info_without_blank_elements = Systemdy::Utility::Formatter.return_an_array_from("5432/tcp  info", argument_splitter: '/') #=> ["5432", "tcp", "info"]

Parameters:

  • string_to_parse (String)

    string to convert to an array

  • argument_splitter (String) (defaults to: '\n')

    character for split string to an array

  • remove_blank_elements (Boolean) (defaults to: true)

    remove blank elements or not

Returns:

  • (Array)

    an array-based list of the values ​​returned by argument_splitter



29
30
31
# File 'lib/systemdy/utility/formatter.rb', line 29

def self.return_an_array_from(string_to_parse, argument_splitter: '\n', remove_blank_elements: true)
    remove_blank_elements ? string_to_parse.split(/#{argument_splitter}/).reject(&:empty?) : string_to_parse.split(/#{argument_splitter}/) 
end

.return_an_array_from_system_command(system_call) ⇒ Array

TODO:

execute system calls with backtick instead of system() beacuse system() return only true or false and not the expected value

method for convert system calls into an array

Examples:

convert a backtick system call into an array

list_of_files_in_my_folder = Systemdy::Utility::Formatter.return_an_array_from_system_command(`ls -la`)

Parameters:

  • system_call (String)

    system call to convert to an array

Returns:

  • (Array)

    an array-based list of the values ​​returned by making a system call



13
14
15
16
17
# File 'lib/systemdy/utility/formatter.rb', line 13

def self.return_an_array_from_system_command(system_call)
    system_call_without_new_line = remove_newline_from_system_command(system_call)    # remove \n from system call returned value
    return system_call_without_new_line if !$?.success?                               # return system error message if the process has non-zero exit status
    return_an_array_from(system_call_without_new_line)                                # convert values returned by `` system call to an array-based list based on argument_splitter value
end