Class: Sigma::Tokens

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/sigma/token.rb

Overview

An ordered collection of Token

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pointerObject

Returns the value of attribute pointer.



227
228
229
# File 'lib/sigma/token.rb', line 227

def pointer
  @pointer
end

Class Method Details

.createTokens

Create an empty collection

Returns:



231
232
233
234
235
# File 'lib/sigma/token.rb', line 231

def self.create
  pointer = FFI::MemoryPointer.new(:pointer)
  ergo_lib_tokens_new(pointer)
  init(pointer)
end

.with_raw_pointer(tokens_ptr) ⇒ Tokens

Note:

A user of sigma_rb generally does not need to call this function

Takes ownership of an existing Tokens Pointer.

Parameters:

  • pointer (FFI::MemoryPointer)

Returns:



241
242
243
# File 'lib/sigma/token.rb', line 241

def self.with_raw_pointer(tokens_ptr)
  init(tokens_ptr)
end

Instance Method Details

#add(token) ⇒ Object

Add to collection. Max capacity of ErgoBox::MAX_TOKENS_COUNT tokens. Will throw error if adding more.

Parameters:



267
268
269
270
# File 'lib/sigma/token.rb', line 267

def add(token)
  error = ergo_lib_tokens_add(token.pointer, self.pointer)
  Util.check_error!(error)
end

#get(index) ⇒ Tokens?

Get item at specified index or return nil if no item exists

Parameters:

  • index (Integer)

Returns:



254
255
256
257
258
259
260
261
262
263
# File 'lib/sigma/token.rb', line 254

def get(index)
  token_pointer = FFI::MemoryPointer.new(:pointer)
  res = ergo_lib_tokens_get(self.pointer, index, token_pointer)
  Util.check_error!(res[:error])
  if res[:is_some]
    Sigma::Token.with_raw_pointer(token_pointer)
  else
    nil
  end
end

#lenInteger

Get length of collection

Returns:

  • (Integer)


247
248
249
# File 'lib/sigma/token.rb', line 247

def len
  ergo_lib_tokens_len(self.pointer)
end