Class: Ciri::Types::UInt

Inherits:
Object
  • Object
show all
Defined in:
lib/ciri/types/uint.rb

Direct Known Subclasses

UInt256, UInt32

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ UInt

Returns a new instance of UInt.



50
51
52
53
# File 'lib/ciri/types/uint.rb', line 50

def initialize(value)
  raise "can't initialize size #{self.class.size} number" if self.class.size <= 0
  @value = value
end

Class Attribute Details

.sizeObject (readonly)

Returns the value of attribute size.



25
26
27
# File 'lib/ciri/types/uint.rb', line 25

def size
  @size
end

Class Method Details

.maxObject



35
36
37
# File 'lib/ciri/types/uint.rb', line 35

def max
  @max ||= 2 ** size - 1
end

.minObject



39
40
41
# File 'lib/ciri/types/uint.rb', line 39

def min
  0
end

.rlp_decode(encoded) ⇒ Object



31
32
33
# File 'lib/ciri/types/uint.rb', line 31

def rlp_decode(encoded)
  Utils.big_endian_decode(RLP.decode(encoded))
end

.rlp_encode(item) ⇒ Object



27
28
29
# File 'lib/ciri/types/uint.rb', line 27

def rlp_encode(item)
  RLP.encode new(item).to_bytes
end

.valid?(n) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/ciri/types/uint.rb', line 43

def valid?(n)
  n >= 0 && n <= max
end

Instance Method Details

#bytes_sizeObject



61
62
63
# File 'lib/ciri/types/uint.rb', line 61

def bytes_size
  self.class.size
end

#serializedObject Also known as: to_bytes



55
56
57
# File 'lib/ciri/types/uint.rb', line 55

def serialized
  Utils.big_endian_encode(@value, size: bytes_size)
end

#to_iObject



65
66
67
# File 'lib/ciri/types/uint.rb', line 65

def to_i
  @value
end