Skip to content

CLibsql::Slice#to_blob fails with uninitialized constant error #18

@fkchang

Description

@fkchang

Problem

When iterating over query results that include blob columns (like F32_BLOB vectors), CLibsql::Slice#to_blob throws:

/lib/libsql.rb:167:in `to_blob': uninitialized constant CLibsql::Slice::Blob (NameError)
        b = Blob.new self[:ptr].read_string self[:len]
            ^^^^

Root Cause

In lib/libsql.rb, the Slice class (line 162) is defined inside the CLibsql module. Its to_blob method (line 166-170) calls Blob.new:

def to_blob
  b = Blob.new self[:ptr].read_string self[:len]
  deinit
  b
end

However, Blob is defined as Libsql::Blob (line 265), not CLibsql::Blob. Ruby's constant lookup searches:

  • CLibsql::Slice::Blob (not found)
  • CLibsql::Blob (not found)
  • ::Blob (not found)

Solution

Change line 167 to use the fully qualified constant:

b = Libsql::Blob.new self[:ptr].read_string self[:len]

Reproduction

require 'libsql'

db = Libsql::Database.new(path: 'test.db')
conn = db.connect

# Create table with blob column
conn.execute("CREATE TABLE test (id INTEGER PRIMARY KEY, data BLOB)")
conn.execute("INSERT INTO test (data) VALUES (?)", ["\x00\x01\x02".b])

# This crashes when iterating results with blob columns
conn.query("SELECT * FROM test").each { |row| p row }

Workaround

Users can apply this monkey-patch before using blobs:

CLibsql.const_set(:Blob, Libsql::Blob) unless defined?(CLibsql::Blob)

Version

turso_libsql 0.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions