Class: Borsh::Buffer

Inherits:
Object
  • Object
show all
Includes:
Readable, Writable
Defined in:
lib/borsh/buffer.rb

Overview

A buffer for reading and writing Borsh data.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Readable

#read_array, #read_bool, #read_enum, #read_f32, #read_f64, #read_i128, #read_i16, #read_i32, #read_i64, #read_i8, #read_map, #read_object, #read_option, #read_result, #read_set, #read_string, #read_struct, #read_u128, #read_u16, #read_u32, #read_u64, #read_u8, #read_unit, #read_vector

Methods included from Writable

#write_array, #write_bool, #write_enum, #write_f32, #write_f64, #write_i128, #write_i16, #write_i32, #write_i64, #write_i8, #write_map, #write_object, #write_option, #write_result, #write_set, #write_string, #write_struct, #write_u128, #write_u16, #write_u32, #write_u64, #write_u8, #write_unit, #write_vector

Constructor Details

#initialize(data = '') {|buffer| ... } ⇒ void

Parameters:

  • data (String, #to_s) (defaults to: '')

Yields:

  • (buffer)

Yield Returns:

  • (void)


29
30
31
32
33
# File 'lib/borsh/buffer.rb', line 29

def initialize(data = '', &block)
  @buffer = StringIO.new(data.to_s)
  @buffer.binmode
  block.call(self) if block_given?
end

Class Method Details

.open(data = nil) {|buffer| ... } ⇒ String

Parameters:

  • data (String, #to_s, nil) (defaults to: nil)

Yields:

  • (buffer)

Yield Returns:

  • (Object)

Returns:

  • (String)


18
19
20
21
22
# File 'lib/borsh/buffer.rb', line 18

def self.open(data = nil, &block)
  buffer = self.new(data || '', &block)
  buffer.close
  buffer.data
end

Instance Method Details

#closevoid

This method returns an undefined value.

Closes the buffer.



54
# File 'lib/borsh/buffer.rb', line 54

def close; @buffer.close; end

#closed?Boolean

Returns ‘true` if the buffer is closed.

Returns:

  • (Boolean)


48
# File 'lib/borsh/buffer.rb', line 48

def closed?; @buffer.closed?; end

#dataString Also known as: string

Returns the buffer data.

Returns:

  • (String)


39
40
41
# File 'lib/borsh/buffer.rb', line 39

def data
  @buffer.string
end

#read(length) ⇒ String

Reads the specified number of bytes from the buffer.

Parameters:

  • length (Integer, #to_i)

Returns:

  • (String)


70
71
72
# File 'lib/borsh/buffer.rb', line 70

def read(length)
  @buffer.read(length.to_i)
end

#write(data) ⇒ Integer

Writes data to the buffer.

Parameters:

  • data (String, #to_s)

Returns:

  • (Integer)


61
62
63
# File 'lib/borsh/buffer.rb', line 61

def write(data)
  @buffer.write(data.to_s)
end