Class: CharacterSet::Character
- Inherits:
-
Object
- Object
- CharacterSet::Character
- Defined in:
- lib/character_set/character.rb
Constant Summary collapse
- ENCODING =
'utf-8'.freeze
- SAFELY_PRINTABLE =
(0x21..0x7E).to_a - %w(- / [ \\ ] ^).map(&:ord)
Instance Attribute Summary collapse
-
#codepoint ⇒ Object
Returns the value of attribute codepoint.
Instance Method Summary collapse
- #escape(opts = {}) ⇒ Object
- #hex ⇒ Object
-
#initialize(codepoint) ⇒ Character
constructor
A new instance of Character.
- #plane ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(codepoint) ⇒ Character
Returns a new instance of Character.
8 9 10 11 12 13 14 |
# File 'lib/character_set/character.rb', line 8 def initialize(codepoint) case codepoint when Integer then self.codepoint = codepoint when String then self.codepoint = codepoint.ord else raise ArgumentError, 'pass an Integer or String' end end |
Instance Attribute Details
#codepoint ⇒ Object
Returns the value of attribute codepoint.
6 7 8 |
# File 'lib/character_set/character.rb', line 6 def codepoint @codepoint end |
Instance Method Details
#escape(opts = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/character_set/character.rb', line 24 def escape(opts = {}) return to_s if SAFELY_PRINTABLE.include?(codepoint) && !opts[:escape_all] return yield(self) if block_given? # https://billposer.org/Software/ListOfRepresentations.html case opts[:format].to_s.downcase.delete('-_ ') when '', 'default', 'es6', 'esnext', 'rb', 'ruby' default_escape(opts) when 'java', 'javascript', 'js' default_escape(opts, false) when 'capitalizableu', 'c#', 'csharp', 'd', 'python' capitalizable_u_escape when 'u+', 'uplus' u_plus_escape when 'literal', 'raw' to_s else raise ArgumentError, "unsupported format: #{opts[:format].inspect}" end end |
#hex ⇒ Object
20 21 22 |
# File 'lib/character_set/character.rb', line 20 def hex codepoint.to_s(16).upcase end |
#plane ⇒ Object
46 47 48 |
# File 'lib/character_set/character.rb', line 46 def plane codepoint / 0x10000 end |
#to_s ⇒ Object
16 17 18 |
# File 'lib/character_set/character.rb', line 16 def to_s codepoint.chr(ENCODING) end |