Class: String

Inherits:
Object show all
Includes:
ActiveSupport::CoreExtensions::String::Inflections
Defined in:
lib/jinx/active_support/core_ext/string.rb,
lib/jinx/helpers/inflector.rb,
lib/jinx/helpers/pretty_print.rb,
lib/jinx/helpers/pretty_print.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from ActiveSupport::CoreExtensions::String::Inflections

#camelize, #classify, #constantize, #dasherize, #demodulize, #foreign_key, #humanize, #parameterize, #pluralize, #singularize, #tableize, #titleize, #underscore

Instance Method Details

#capitalize_firstString

Returns this String with the first letter capitalized and other letters preserved.

Examples:

"rosesAreRed".capitalize_first #=> "RosesAreRed"

Returns:

  • (String)

    this String with the first letter capitalized and other letters preserved



17
18
19
# File 'lib/jinx/helpers/inflector.rb', line 17

def capitalize_first
  sub(/(?:^)(.)/) { $1.upcase }
end

#decapitalizeString

Returns this String with the first letter decapitalized and other letters preserved.

Examples:

"RosesAreRed".decapitalize #=> "rosesAreRed"

Returns:

  • (String)

    this String with the first letter decapitalized and other letters preserved



33
34
35
# File 'lib/jinx/helpers/inflector.rb', line 33

def decapitalize
  sub(/(?:^)(.)/) { $1.downcase }
end

#indefinitizeString

Returns this String with a leading indefinite article.

Examples:

"rose".indefinitize #=> "a rose"
"eagle".indefinitize #=> "an eagle"

Returns:

  • (String)

    this String with a leading indefinite article



25
26
27
28
# File 'lib/jinx/helpers/inflector.rb', line 25

def indefinitize
  article = self =~ /^[aeiou]/i ? 'an' : 'a'
  "#{article} #{self}"
end

#pretty_print(q) ⇒ Object

Pretty-prints this String using the Object pretty_print rather than Enumerable pretty_print.



179
180
181
# File 'lib/jinx/helpers/pretty_print.rb', line 179

def pretty_print(q)
  q.text self
end

#quantify(quantity) ⇒ String

Returns this String qualified by a plural if the quantity is not 1.

Examples:

"rose".quantify(3) #=> "roses"
"rose".quantify(1 #=> "rose"

Parameters:

  • quantity (Numeric)

    the amount qualifier

Returns:

  • (String)

    this String qualified by a plural if the quantity is not 1

Raises:

  • (ArgumentError)


9
10
11
12
# File 'lib/jinx/helpers/inflector.rb', line 9

def quantify(quantity)
  raise ArgumentError.new("Missing quantity argument") if quantity.nil?
  "#{quantity} #{quantity == 1 ? self : pluralize}"
end