Class: NRSER::Char::Special
Overview
Lil’ structure with useful info and methods for special characters.
Instance Attribute Summary collapse
-
#caret ⇒ nil, String
readonly
If the character is a control character, the “caret” (‘^X`) style replacement for it.
-
#char ⇒ String
readonly
The character as a length-one string (UTF-8 encoding).
-
#names ⇒ Array<String>
readonly
Zero or more strings names for the character.
-
#symbol ⇒ nil, String
(also: #picture)
readonly
A printable Unicode “control picture” character that can be used as a replacement character for control characters.
Instance Method Summary collapse
-
#ascii? ⇒ Boolean
‘true` if the character code-point is in the ASCII range.
-
#control? ⇒ Boolean
‘true` if the character is a control character.
-
#dec ⇒ Fixnum
(also: #ord)
Decimal encoding of the character (with respect to UTF-8).
- #esc_seq ⇒ Object
-
#hex ⇒ String
Hex string as it would appear in ‘uXXXX`.
-
#initialize(char:, names: [], caret: nil, symbol: nil) ⇒ Special
constructor
Instantiate a new ‘Special`.
-
#name ⇒ nil, String
The first of #names (if any).
-
#replace(string, with: :symbol) ⇒ String
Replace all occurrences of #char in the string.
Constructor Details
#initialize(char:, names: [], caret: nil, symbol: nil) ⇒ Special
Instantiate a new ‘Special`.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/nrser/char/special.rb', line 107 def initialize char:, names: [], caret: nil, symbol: nil unless char.is_a?( String ) && char.length == 1 raise ArgumentError, "char must be string of length 1" end @char = char.freeze @names = names.map { |n| n.to_s.freeze }.freeze @caret = caret.freeze @symbol = symbol.freeze end |
Instance Attribute Details
#caret ⇒ nil, String (readonly)
If the character is a control character, the “caret” (‘^X`) style replacement for it.
42 43 44 |
# File 'lib/nrser/char/special.rb', line 42 def caret @caret end |
#char ⇒ String (readonly)
The character as a length-one string (UTF-8 encoding).
19 20 21 |
# File 'lib/nrser/char/special.rb', line 19 def char @char end |
#names ⇒ Array<String> (readonly)
Zero or more strings names for the character.
26 27 28 |
# File 'lib/nrser/char/special.rb', line 26 def names @names end |
#symbol ⇒ nil, String (readonly) Also known as: picture
A printable Unicode “control picture” character that can be used as a replacement character for control characters.
The nice things about these is they:
-
Should allow for replacement without changing the Ruby string length*.
> * I say should because lengths of unicode strings can get funky > across different platforms due to bytes and code points and glyphs > and a bunch of other stuff it seems like only about three people > fully understand.. it often will change the *byte length*, and > who knows how that will go if you start handing it back and forth > to C or something else.
-
Visually represent the replaced character. You know what it was without having to remember caret or hex representations.
-
Have a far lower chance of ambiguity versus #carat, #esc_seq, etc. (is a control character or did the string really have a ‘^X` in it?).
Drawbacks:
-
They’re all gonna be outside the ASCII range, so if you are for some reason stuck with something that can’t Unicode you’re gonna get some gibberish at best, if not outright breakage.
-
They require font support, or you’re going to probably get one of those little box things that we used to see all over the web before browsers and operating systems managed to get their shit together.
84 85 86 |
# File 'lib/nrser/char/special.rb', line 84 def symbol @symbol end |
Instance Method Details
#ascii? ⇒ Boolean
Returns ‘true` if the character code-point is in the ASCII range.
170 171 172 |
# File 'lib/nrser/char/special.rb', line 170 def ascii? char.ascii_only? end |
#control? ⇒ Boolean
Returns ‘true` if the character is a control character.
158 159 160 |
# File 'lib/nrser/char/special.rb', line 158 def control? @control ||= NRSER::Char.control?( char ) end |
#dec ⇒ Fixnum Also known as: ord
Decimal encoding of the character (with respect to UTF-8).
Equivalent to ‘.char.ord`.
142 143 144 |
# File 'lib/nrser/char/special.rb', line 142 def dec char.ord end |
#esc_seq ⇒ Object
163 164 165 |
# File 'lib/nrser/char/special.rb', line 163 def esc_seq @esc_seq ||= char.inspect[1..-2].freeze end |
#hex ⇒ String
Returns Hex string as it would appear in ‘uXXXX`.
151 152 153 |
# File 'lib/nrser/char/special.rb', line 151 def hex @hex ||= ("%04X" % char.ord).freeze end |