Class: NRSER::Char::AlphaNumericSub
- Defined in:
- lib/nrser/char/alpha_numeric_sub.rb
Overview
Lil’ structure with useful info and methods for special characters.
Instance Attribute Summary collapse
-
#exceptions ⇒ Hash<String, String>
readonly
Map of source character to dest character substitutions to handle cases where a few of the destination alpha-numerics don’t follow the “offest from ‘a`/`A`/`0`” rule.
-
#sub_0 ⇒ String?
readonly
The target set’s ‘0` character (as a length 1 String), the `#ord` of which is used to determine the target `#ord` of numeric `0-9` characters for substitution.
-
#sub_A ⇒ String?
readonly
The ‘A` character (as a length 1 String), the `#ord` of which is used to determine the target `#ord` upper-case alpha (`A-Z`) characters for substitution.
-
#sub_a ⇒ String?
readonly
The ‘a` character (as a length 1 String), the `#ord` of which is used to determine the target `#ord` of lower-case alpha (`a-z`) characters for substitution.
On-Demand Built-In Instances (Class Methods) collapse
-
.unicode_math_bold ⇒ NRSER::Char::AlphaNumericSub
Instance to substitute alphas with their “Unicode Math Bold” counterpart.
-
.unicode_math_bold_italic ⇒ NRSER::Char::AlphaNumericSub
Instance to substitute alphas with their “Unicode Math Bold Italic” counterpart.
-
.unicode_math_italic ⇒ NRSER::Char::AlphaNumericSub
Instance to substitute alphas with their “Unicode Math Italic” counterpart.
-
.unicode_math_monospace ⇒ NRSER::Char::AlphaNumericSub
Instance to substitute alphas with their “Unicode Math Bold Italic” counterpart.
Instance Method Summary collapse
- #demo ⇒ Object
-
#initialize(sub_a: nil, sub_A: nil, sub_0: nil, exceptions: nil) ⇒ AlphaNumericSub
constructor
Instantiate a new ‘AlphaNumericSub`.
-
#sub(src) ⇒ Object
Instance Methods ======================================================================.
Constructor Details
#initialize(sub_a: nil, sub_A: nil, sub_0: nil, exceptions: nil) ⇒ AlphaNumericSub
Instantiate a new ‘AlphaNumericSub`.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 158 def initialize sub_a: nil, sub_A: nil, sub_0: nil, exceptions: nil binding.locals.tap do |args| t.list( t.utf8_char? ).check \ args.slice( :sub_a, :sub_A, :sub_0 ).values t.hash_?( keys: t.utf8_char, values: t.utf8_char ).check exceptions if args.all?( &:nil? ) raise ArgumentError.new, "All arguments can't be `nil` (sub couldn't do anything)" end end @sub_a = sub_a.freeze @sub_A = sub_A.freeze @sub_0 = sub_0.freeze @exceptions = if exceptions exceptions.each { |k, v| k.freeze; v.freeze }.freeze end end |
Instance Attribute Details
#exceptions ⇒ Hash<String, String> (readonly)
Map of source character to dest character substitutions to handle cases where a few of the destination alpha-numerics don’t follow the “offest from ‘a`/`A`/`0`” rule.
68 69 70 |
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 68 def exceptions @exceptions end |
#sub_0 ⇒ String? (readonly)
The target set’s ‘0` character (as a length 1 String), the `#ord` of which is used to determine the target `#ord` of numeric `0-9` characters for substitution.
58 59 60 |
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 58 def sub_0 @sub_0 end |
Class Method Details
.unicode_math_bold ⇒ NRSER::Char::AlphaNumericSub
Instance to substitute alphas with their “Unicode Math Bold” counterpart.
103 104 105 106 107 108 |
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 103 def self.unicode_math_bold @@unicode_math_italic ||= new \ sub_a: '𝐚', sub_A: '𝐀', sub_0: '𝟬' end |
.unicode_math_bold_italic ⇒ NRSER::Char::AlphaNumericSub
Instance to substitute alphas with their “Unicode Math Bold Italic” counterpart.
116 117 118 119 120 121 122 123 |
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 116 def self.unicode_math_bold_italic @@unicode_math_italic ||= new \ sub_A: '𝑨', sub_a: '𝒂', # Just use the bold numbers since Unicode doesn't seem to have # italic numbers sub_0: '𝟬' end |
.unicode_math_italic ⇒ NRSER::Char::AlphaNumericSub
Instance to substitute alphas with their “Unicode Math Italic” counterpart.
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 86 def self.unicode_math_italic @@unicode_math_italic ||= new \ sub_a: '𝑎', sub_A: '𝐴', # Doesn't have italic numbers to just don't sub exceptions: { # The `h` is not in the run 'h' => 'ℎ' } end |
.unicode_math_monospace ⇒ NRSER::Char::AlphaNumericSub
Instance to substitute alphas with their “Unicode Math Bold Italic” counterpart.
131 132 133 134 135 136 |
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 131 def self.unicode_math_monospace @@unicode_math_italic ||= new \ sub_A: '𝙰', sub_a: '𝚊', sub_0: '𝟶' end |
Instance Method Details
#demo ⇒ Object
218 219 220 |
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 218 def demo sub ['a'..'z', 'A'..'Z', '0'..'9'].map { |_| _.to_a.join }.join end |
#sub(src) ⇒ Object
Instance Methods
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/nrser/char/alpha_numeric_sub.rb', line 186 def sub src dest = src.dup if @exceptions @exceptions.each do |src_char, dest_char| dest.gsub! src_char, dest_char end end [ ['a', /[a-z]/], ['A', /[A-Z]/], ['0', /[0-9]/], ].each do |src_char, regexp| src_char_ord = src_char.ord start_dest_char = instance_variable_get "@sub_#{ src_char }" unless start_dest_char.nil? start_dest_char_ord = start_dest_char.ord dest.gsub!( regexp ) { |char| NRSER::Char.from_i( start_dest_char_ord + (char.ord - src_char_ord) ) } end end dest end |