Module: Lazier::String
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/lazier/string.rb
Overview
Extensions for the String class.
Instance Method Summary (collapse)
-
- (String) ensure_valid_utf8(replacement = "")
Makes sure the string only contains valid UTF-8 sequences.
-
- (Object) remove_accents
Removes accents from the string, normalizing to the normal letter.
-
- (String) replace_ampersands
Returns the string with all
&
replaced with&
. -
- (String) untitleize
Returns the tagged version of a string.
-
- (String) value
Returns the string itself for use in form helpers.
Instance Method Details
- (String) ensure_valid_utf8(replacement = "")
Makes sure the string only contains valid UTF-8 sequences.
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"
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 &
.
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"
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.
57 58 59 |
# File 'lib/lazier/string.rb', line 57 def value self end |