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.
-
- (Array) tokenize(no_blanks: true, strip: true, no_duplicates: false, pattern: /\s*,\s*/, presence_method: :present?)
Splits a string containing tokens using a specified pattern and applying some sanitizations.
-
- (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.
15 16 17 18 |
# File 'lib/lazier/string.rb', line 15 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 encode("utf-16", invalid: :replace, undef: :replace, replace: replacement).encode("utf-8") end |
- (Object) remove_accents
Removes accents from the string, normalizing to the normal letter.
ruby
"èòàù".remove_accents
# => "eoau"
51 52 53 |
# File 'lib/lazier/string.rb', line 51 def remove_accents ::I18n.transliterate(self) end |
- (Array) tokenize(no_blanks: true, strip: true, no_duplicates: false, pattern: /\s*,\s*/, presence_method: :present?)
Splits a string containing tokens using a specified pattern and applying some sanitizations.
35 36 37 38 39 40 41 |
# File 'lib/lazier/string.rb', line 35 def tokenize(no_blanks: true, strip: true, no_duplicates: false, pattern: /\s*,\s*/, presence_method: :present?) rv = split(pattern) rv.map!(&:strip) if strip rv.select!(&presence_method) if no_blanks rv.uniq! if no_duplicates rv end |
- (String) value
Returns the string itself for use in form helpers.
23 24 25 |
# File 'lib/lazier/string.rb', line 23 def value self end |