Module: SleepingKingStudios::Tools::StringTools

Extended by:
StringTools
Included in:
StringTools
Defined in:
lib/sleeping_king_studios/tools/string_tools.rb

Overview

Tools for working with strings.

Instance Method Summary collapse

Instance Method Details

#pluralize(count, single, plural) ⇒ String

Returns the singular or the plural value, depending on the provided item count.

Examples:

"There are four #{StringTools.pluralize 4, 'light', 'lights'}!"
#=> 'There are four lights!'

Parameters:

  • count (Integer)

    The number of items.

  • single (String)

    The singular form of the word or phrase.

  • plural (String)

    The plural form of the word or phrase.

Returns:

  • (String)

    The single form if count == 1; otherwise the plural form.



23
24
25
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 23

def pluralize count, single, plural
  1 == count ? single : plural
end

#underscore(str) ⇒ String

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

Parameters:

  • str (String)

    The string to convert.

Returns:

  • (String)

    The converted string.

See Also:

  • ActiveSupport::Inflector#underscore.


35
36
37
38
39
40
41
42
43
44
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 35

def underscore str
  require_string! str

  str = str.dup
  str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  str.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  str.tr!("-", "_")
  str.downcase!
  str
end