Module: Lazier::String

Extended by:
ActiveSupport::Concern
Defined in:
lib/lazier/string.rb

Overview

Extensions for the String class.

Instance Method Summary (collapse)

Instance Method Details

- (String) ensure_valid_utf8(replacement = "")

Makes sure the string only contains valid UTF-8 sequences.

Parameters:

  • replacement (String) (defaults to: "")

    The string to use to replace invalid sequences.

Returns:

  • (String)

    The string with any invalid UTF-8 sequences replaced.



28
29
30
31
# File 'lib/lazier/string.rb', line 28

def ensure_valid_utf8(replacement = "")
  # This odd line is because if need to specify a different encoding (without losing infos) to replace invalid bytes and then we go back to utf-8
  !defined?(JRUBY_VERSION) ? encode("utf-16", invalid: :replace, undef: :replace, replace: replacement).encode("utf-8") : raise(RuntimeError.new("Sorry, Lazier::String#ensure_valid_utf8 is not available on JRuby."))
end

- (Object) remove_accents

Removes accents from the string, normalizing to the normal letter.

ruby "èòàù".remove_accents # => "eoau"

Returns:

  • The string with all accents removed.



20
21
22
# File 'lib/lazier/string.rb', line 20

def remove_accents
  silence_warnings { mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, "").to_s }
end

- (String) replace_ampersands

Returns the string with all & replaced with &.

Returns:

  • (String)

    The string with all & replaced with &.



50
51
52
# File 'lib/lazier/string.rb', line 50

def replace_ampersands
  gsub(/&(\S+);/, "&\\1;")
end

- (String) untitleize

Returns the tagged version of a string.

The string is downcased and spaces are substituted with -.

ruby "ABC cde".untitleize # => "abc-cde"

Returns:

  • (String)

    The untitleized version of the string.



43
44
45
# File 'lib/lazier/string.rb', line 43

def untitleize
  downcase.gsub(" ", "-")
end

- (String) value

Returns the string itself for use in form helpers.

Returns:

  • (String)

    The string itself.



57
58
59
# File 'lib/lazier/string.rb', line 57

def value
  self
end