Class: SolanaRuby::DataTypes::Blob

Inherits:
Object
  • Object
show all
Defined in:
lib/solana_ruby/data_types/blob.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Blob

Constructor to initialize size of the blob

Raises:

  • (ArgumentError)


7
8
9
10
# File 'lib/solana_ruby/data_types/blob.rb', line 7

def initialize(size)
  raise ArgumentError, "Size must be a positive integer" unless size.is_a?(Integer) && size > 0
  @size = size
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



4
5
6
# File 'lib/solana_ruby/data_types/blob.rb', line 4

def size
  @size
end

Instance Method Details

#deserialize(bytes) ⇒ Object

Deserialize a byte array into the original object format

Raises:

  • (ArgumentError)


22
23
24
25
26
27
# File 'lib/solana_ruby/data_types/blob.rb', line 22

def deserialize(bytes)
  # Ensure the byte array is of the correct size
  raise ArgumentError, "Byte array size must match the expected size" unless bytes.length == @size

  bytes.pack('C*')
end

#serialize(obj) ⇒ Object

Serialize the given object to a byte array

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
# File 'lib/solana_ruby/data_types/blob.rb', line 13

def serialize(obj)
  # Ensure obj is an array and then convert to byte array
  obj = [obj] unless obj.is_a?(Array)
  raise ArgumentError, "Object must be an array of bytes" unless obj.all? { |e| e.is_a?(Integer) && e.between?(0, 255) }

  obj.pack('C*').bytes
end