Tools

A library of utility services and concerns.

Class: SleepingKingStudios::Tools::StringTools

Parent Namespace
SleepingKingStudios::Tools
Inherited Classes
SleepingKingStudios::Tools::Base > Object
Defined In
lib/sleeping_king_studios/tools/string_tools.rb

Table Of Contents

Overview

Tools for working with strings.

Back To Top

Class Methods

.instance => SleepingKingStudios::Tools::Base

Inherited From
SleepingKingStudios::Tools::Base

Returns

Back To Top

Constructor

#initialize(inflector: nil) => StringTools

Parameters

Returns

Back To Top

Instance Attributes

#inflector => Object (readonly)

Returns

Back To Top

Instance Methods

#camelize(str) => String

Converts a lowercase, underscore-separated string to CamelCase.

Examples

StringTools#camelize 'valhalla'
#=> 'Valhalla'

StringTools#camelize 'muspelheimr_and_niflheimr'
#=> 'MuspelheimrAndNiflheimr'

Parameters

Returns

See Also

#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.

Examples

# Equivalent to `StringTools.underscore(StringTools.pluralize str)`.
StringTools#chain 'ArchivedPeriodical', :underscore, :pluralize
# => 'archived_periodicals'

Parameters

Returns

#indent(str, count = 2) => String

Adds the specified number of spaces to the start of each line.

Examples

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"

Parameters

Returns

#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.

Examples

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"

Parameters

Yield Parameters

Yield Returns

Returns

#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.

Examples

StringTools.plural? 'light'
#=> false

StringTools.plural? 'lights'
#=> true

Parameters

Returns

See Also

#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.

Examples

StringTools.pluralize 'light'
#=> 'lights'

Parameters

Returns

See Also

#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.

Examples

StringTools.singular? 'light'
#=> true

StringTools.singular? 'lights'
#=> false

Parameters

Returns

See Also

#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.

Examples

StringTools.singularize 'lights'
#=> 'light'

Parameters

Returns

#string?(str) => Boolean

Returns true if the object is a String.

Examples

StringTools.string?(nil)
#=> false
StringTools.string?([])
#=> false
StringTools.string?('Greetings, programs!')
#=> true
StringTools.string?(:greetings_starfighter)
#=> false

Parameters

Returns

#underscore(str) => String

Converts a mixed-case string to a lowercase, underscore separated string.

Examples

StringTools#underscore 'Bifrost'
#=> 'bifrost'

StringTools#underscore 'FenrisWolf'
#=> 'fenris_wolf'

Parameters

Returns

See Also

Back To Top


Back to Documentation | Reference | SleepingKingStudios | SleepingKingStudios::Tools