Class: Postscript::Model::Literals::HexLiteral

Inherits:
Object
  • Object
show all
Defined in:
lib/postscript/model/literals/hex.rb

Overview

Hex-string literal: <DEADBEEF>.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ HexLiteral

Returns a new instance of HexLiteral.



10
11
12
13
# File 'lib/postscript/model/literals/hex.rb', line 10

def initialize(value)
  @value = value.to_s
  freeze
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/postscript/model/literals/hex.rb', line 8

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



26
27
28
# File 'lib/postscript/model/literals/hex.rb', line 26

def ==(other)
  other.is_a?(HexLiteral) && other.value == value
end

#accept(visitor, ctx) ⇒ Object



22
23
24
# File 'lib/postscript/model/literals/hex.rb', line 22

def accept(visitor, ctx)
  visitor.visit_hex(self, ctx)
end

#bytesObject

Raw bytes encoded by the hex literal.



16
17
18
19
20
# File 'lib/postscript/model/literals/hex.rb', line 16

def bytes
  hex = value.delete("^0-9a-fA-F")
  hex << "0" if hex.length.odd?
  hex.chars.each_slice(2).map { |pair| pair.join.to_i(16) }.pack("C*")
end

#hashObject



31
32
33
# File 'lib/postscript/model/literals/hex.rb', line 31

def hash
  value.hash
end