Class: BinaryCodec::Hash
- Inherits:
-
ComparableSerializedType
- Object
- SerializedType
- ComparableSerializedType
- BinaryCodec::Hash
- Defined in:
- lib/binary-codec/types/hash.rb
Instance Attribute Summary
Attributes inherited from SerializedType
Class Method Summary collapse
Instance Method Summary collapse
- #compare_to(other) ⇒ Object
-
#initialize(bytes, width) ⇒ Hash
constructor
A new instance of Hash.
-
#nibblet(depth) ⇒ Integer
Returns four bits at the specified depth within a hash.
Methods inherited from ComparableSerializedType
Methods inherited from SerializedType
from_bytes, from_hex, from_json, get_type_by_name, #to_byte_sink, #to_bytes, #to_hex, #to_json, #to_s
Constructor Details
#initialize(bytes, width) ⇒ Hash
Returns a new instance of Hash.
6 7 8 9 10 11 12 13 |
# File 'lib/binary-codec/types/hash.rb', line 6 def initialize(bytes, width) @bytes = bytes @width = width if bytes.length != @width # raise StandardError, "Invalid Hash length #{bytes.length} for width #{@width}" raise StandardError, "Invalid Hash length #{bytes.length}" end end |
Class Method Details
.from(hex_string) ⇒ Object
15 16 17 |
# File 'lib/binary-codec/types/hash.rb', line 15 def self.from(hex_string) new(hex_to_bytes(hex_string)) end |
.from_parser(parser, hint = nil) ⇒ Object
19 20 21 |
# File 'lib/binary-codec/types/hash.rb', line 19 def self.from_parser(parser, hint = nil) new(parser.read(hint || width)) end |
Instance Method Details
#compare_to(other) ⇒ Object
23 24 25 |
# File 'lib/binary-codec/types/hash.rb', line 23 def compare_to(other) @bytes <=> other.bytes end |
#nibblet(depth) ⇒ Integer
Returns four bits at the specified depth within a hash
31 32 33 34 35 36 37 38 39 |
# File 'lib/binary-codec/types/hash.rb', line 31 def nibblet(depth) byte_index = depth > 0 ? (depth / 2).floor : 0 b = bytes[byte_index] if depth.even? (b & 0xf0) >> 4 else b & 0x0f end end |