Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ Add String#ascii_only & #ascii_only!
- Loading branch information
1 parent
d1ec550
commit a4f35e8
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# encoding: utf-8 | ||
|
||
class String | ||
# Return a copy of string with ASCII characters only | ||
# Source: http://stackoverflow.com/questions/1268289/how-to-get-rid-of-non-ascii-characters-in-ruby | ||
# | ||
# @return [String] a copy of string with ASCII characters only | ||
# | ||
# @example | ||
# 'abc'.ascii_only #=> 'abc' | ||
# | ||
# @example | ||
# '中文123'.ascii_only #=> '123' | ||
unless String.method_defined? :ascii_only | ||
def ascii_only | ||
dup.ascii_only! | ||
end | ||
end | ||
|
||
# Modify self and keep ASCII characters only | ||
# Returns the string even if no changes were made. | ||
# Source: http://stackoverflow.com/questions/1268289/how-to-get-rid-of-non-ascii-characters-in-ruby | ||
# | ||
# @return [String] The result string | ||
# | ||
# @example | ||
# 'abc'.ascii_only! #=> 'abc' | ||
# | ||
# @example | ||
# '中文123'.ascii_only! #=> '123' | ||
unless String.method_defined? :ascii_only! | ||
def ascii_only! | ||
encoding_options = { | ||
:invalid => :replace, # Replace invalid byte sequences | ||
:undef => :replace, # Replace anything not defined in ASCII | ||
:replace => '', # Use a blank for those replacements | ||
:UNIVERSAL_NEWLINE_DECORATOR => true # Always break lines with \n | ||
} | ||
self.encode! Encoding.find('ASCII'), encoding_options | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe 'String#ascii_only' do | ||
it 'returns same value for string with ASCII chars only' do | ||
expect('abc'.ascii_only).to eq 'abc' | ||
end | ||
|
||
it 'returns string without non-ASCII chars' do | ||
expect("abc\u{6666}".force_encoding("UTF-8").ascii_only).to eq 'abc' | ||
end | ||
|
||
it 'returns string without non-ASCII chars and with ASCII chars' do | ||
expect("\u{6666}".force_encoding("UTF-8").ascii_only).to eq '' | ||
end | ||
|
||
it 'does not change the original string' do | ||
string = "abc\u{6666}".force_encoding("UTF-8") | ||
string.ascii_only | ||
expect(string).to eq "abc\u{6666}".force_encoding("UTF-8") | ||
end | ||
end | ||
|
||
describe 'String#ascii_only!' do | ||
it 'returns same value for string with ASCII chars only' do | ||
expect('abc'.ascii_only!).to eq 'abc' | ||
end | ||
|
||
it 'returns string without non-ASCII chars' do | ||
expect("abc\u{6666}".force_encoding("UTF-8").ascii_only!).to eq 'abc' | ||
end | ||
|
||
it 'returns string without non-ASCII chars and with ASCII chars' do | ||
expect("\u{6666}".force_encoding("UTF-8").ascii_only!).to eq '' | ||
end | ||
|
||
it 'changes the original string' do | ||
string = "abc\u{6666}".force_encoding("UTF-8") | ||
string.ascii_only! | ||
expect(string).to eq 'abc' | ||
end | ||
end |