module StringExtensions

Public Instance Methods

ordinal() click to toggle source
# File lib/extras/extensions.rb, line 11
def ordinal
  ActiveSupport::Inflector.ordinal(self)
end
ordinalize() click to toggle source

Adds ‘th’, ‘nd’, ‘st’ like ordinal to numerical (string) values. e.g. 22nd, 40th, 1st etc.

# File lib/extras/extensions.rb, line 4
def ordinalize
  match = /\A(?<int>\d+)\Z|\A(?<real>\d+\.\d+)\Z/.match(self)
  raise NameError.new("cannot ordinalize non-numeric value") unless match
  num = match[:int] ? self.to_i : self.to_f
  "#{num}#{num.to_s.ordinal}"
end
squish() click to toggle source

Removes extra whitespaces from the string.

# File lib/extras/extensions.rb, line 16
def squish
  dup.squish!
end
squish!() click to toggle source

Destructive version of ‘squish` method.

# File lib/extras/extensions.rb, line 21
def squish!
  strip!
  gsub!(/\s+/, ' ')
  self
end