A library of utility services and concerns.
Tools for working with strings.
.instance => SleepingKingStudios::Tools::Base
#initialize(inflector: nil) => StringTools
#inflector => Object (readonly)
#camelize(str) => String
Converts a lowercase, underscore-separated string to CamelCase.
StringTools#camelize 'valhalla'
#=> 'Valhalla'
StringTools#camelize 'muspelheimr_and_niflheimr'
#=> 'MuspelheimrAndNiflheimr'
#chain(str, *commands) => String
Performs a series of operations on the string.
Use #chain to call each specified method in the chain in sequence, passing the output of each method to the next method.
# Equivalent to `StringTools.underscore(StringTools.pluralize str)`.
StringTools#chain 'ArchivedPeriodical', :underscore, :pluralize
# => 'archived_periodicals'
#indent(str, count = 2) => String
Adds the specified number of spaces to the start of each line.
string = 'The Hobbit'
StringTools.indent(string)
#=> ' The Hobbit'
titles = [
"The Fellowship of the Ring",
"The Two Towers",
"The Return of the King"
]
string = titles.join "\n"
StringTools.indent(string, 4)
#=> " The Fellowship of the Ring\n"\
" The Two Towers\n"\
" The Return of the King"
#map_lines(str) => String
Yields each line to the provided block and combines the results.
The results of each line are combined back into a new multi-line string.
string = 'The Hobbit'
StringTools.map_lines(string) { |line| " #{line}" }
#=> '- The Hobbit'
titles = [
"The Fellowship of the Ring",
"The Two Towers",
"The Return of the King"
]
string = titles.join "\n"
StringTools.map_lines(string) { |line, index| "#{index}. #{line}" }
#=> "0. The Fellowship of the Ring\n"\
"1. The Two Towers\n"\
"2. The Return of the King"
#plural?(word) => Boolean
Determines whether or not the given word is in plural form.
If calling #pluralize(word) is equal to word, the word is considered plural.
StringTools.plural? 'light'
#=> false
StringTools.plural? 'lights'
#=> true
#pluralize(str) => String
Takes a word in singular form and returns the plural form.
This method delegates to the configured inflector, which converts the given word based on the defined rules and known irregular/uncountable words.
StringTools.pluralize 'light'
#=> 'lights'
#singular?(word) => Boolean
Determines whether or not the given word is in singular form.
If calling #singularize(word) is equal to word, the word is considered singular.
StringTools.singular? 'light'
#=> true
StringTools.singular? 'lights'
#=> false
#singularize(str) => String
Transforms the word to a singular, lowercase form.
This method delegates to the configured inflector, which converts the given word based on the defined rules and known irregular/uncountable words.
StringTools.singularize 'lights'
#=> 'light'
#string?(str) => Boolean
Returns true if the object is a String.
StringTools.string?(nil)
#=> false
StringTools.string?([])
#=> false
StringTools.string?('Greetings, programs!')
#=> true
StringTools.string?(:greetings_starfighter)
#=> false
#underscore(str) => String
Converts a mixed-case string to a lowercase, underscore separated string.
StringTools#underscore 'Bifrost'
#=> 'bifrost'
StringTools#underscore 'FenrisWolf'
#=> 'fenris_wolf'
Back to Documentation | Reference | SleepingKingStudios | SleepingKingStudios::Tools