Class: StringUtility

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

Instance Method Summary collapse

Constructor Details

#initializeStringUtility

Creates a new instance of the StringUtility class



4
# File 'lib/utils.rb', line 4

def initialize; end

Instance Method Details

#separate_with(string, count = 3, separator = ',') ⇒ String

Separates the string by another string. Useful for converting integers into human-readable numbers.

Parameters:

  • string (String)

    The string to separate.

  • count (Int) (defaults to: 3)

    The number of integers to separate by. Defaults to 3.

  • separator (String) (defaults to: ',')

    The string to use as a separator. Can only be 1 character long. Will use the first character if it is greater than 1 character long. Defaults to a comma.

Returns:

  • (String)

    Formatted version of the provided string.



11
12
13
14
15
16
17
18
19
20
# File 'lib/utils.rb', line 11

def separate_with(string, count = 3, separator = ',')
  if separator.length > 1
    separator = separator[0]
  end
  string = string.reverse!
  array = string.scan(/.{1,#{count}}/)
  string = array.join(separator)
  string = string.reverse!
  return string
end