Sha256: e3bb2e2fb80b60d74de3d45c7b0d71b628bfd74dff2e2dee510e9a6a5b52f3dd
Contents?: true
Size: 1.66 KB
Versions: 3
Compression:
Stored size: 1.66 KB
Contents
class String # Get a new string with non-ASCII characters removed. # # alt - String to replace non-ASCII characters with. # Defaults to a blank string (`''`). # # Examples # # 'abc'.ascii_only #=> 'abc' # '中文123'.ascii_only #=> '123' # # Returns a copy of [String] with ASCII characters only. # # CREDIT: Nathan Long # # SEE: http://stackoverflow.com/questions/1268289 def ascii_only(alt='') encoding_options = { :invalid => :replace, # Replace invalid byte sequences :undef => :replace, # Replace anything not defined in ASCII :replace => alt, # Use a blank for those replacements :UNIVERSAL_NEWLINE_DECORATOR => true # Always break lines with \n } self.encode(Encoding.find('ASCII'), encoding_options) end # Modify string keeping only ASCII characters. # # alt - String to replace non-ASCII characters with. # Defaults to a blank string (`''`). # # Examples # # 'abc'.ascii_only! #=> 'abc' # '中文123'.ascii_only! #=> '123' # # Returns [String] # # CREDIT: Nathan Long # # SEE: http://stackoverflow.com/questions/1268289 def ascii_only!(alt='') encoding_options = { :invalid => :replace, # Replace invalid byte sequences :undef => :replace, # Replace anything not defined in ASCII :replace => alt, # Use a blank for those replacements :UNIVERSAL_NEWLINE_DECORATOR => true # Always break lines with \n } self.encode!(Encoding.find('ASCII'), encoding_options) end end
Version data entries
3 entries across 3 versions & 2 rubygems
Version | Path |
---|---|
facets-glimmer-3.2.0 | lib/core/facets/string/ascii_only.rb |
facets-3.1.0 | lib/core/facets/string/ascii_only.rb |
facets-3.0.0 | lib/core/facets/string/ascii_only.rb |