Method: BSON::Binary.from_vector

Defined in:
lib/bson/binary.rb

.from_vector(vector, dtype = nil, padding = 0, validate_vector_data: false) ⇒ BSON::Binary

be ignored when a vector element’s size is less than a byte. Must be 0 if vector is a BSON::Vector.

Parameters:

  • vector (BSON::Vector | Array)

    The vector data.

  • dtype (Symbol | nil) (defaults to: nil)

    The vector data type, must be nil if vector is a BSON::Vector.

  • padding (Integer) (defaults to: 0)

    The number of bits in the final byte that are to

  • validate_vector_data (Boolean) (defaults to: false)

    Whether to validate the vector data.

Returns:

Since:

  • 2.0.0



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/bson/binary.rb', line 412

def self.from_vector(vector, dtype = nil, padding = 0, validate_vector_data: false)
  data, dtype, padding = extract_args_for_vector(vector, dtype, padding)
  validate_args_for_vector!(data, dtype, padding)

  format = case dtype
           when :int8 then 'c*'
           when :float32 then 'f*'
           when :packed_bit then 'C*'
           else raise ArgumentError, "Unsupported type: #{dtype}"
           end
  if validate_vector_data
    validate_vector_data!(data, dtype)
  end
   = [ VECTOR_DATA_TYPES[dtype], padding ].pack('CC')
  data = data.pack(format)
  new(.concat(data), :vector)
end