Class: FFI::Encoding

Inherits:
Object
  • Object
show all
Includes:
DataConverter
Defined in:
lib/ffi/encoding.rb

Overview

DataConverter for reading/writing encoded strings

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encoding) ⇒ Encoding

Returns a new instance of Encoding.



34
35
36
# File 'lib/ffi/encoding.rb', line 34

def initialize(encoding)
  @encoding = encoding
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



10
11
12
# File 'lib/ffi/encoding.rb', line 10

def encoding
  @encoding
end

Class Method Details

.for(encoding) ⇒ Object

Create a dataconverter for the specified encoding

Examples:

module MyFFIModule
  extend FFI::Library
  typedef FFI::Encoding.for('utf8'), :utf8_string
end


18
19
20
# File 'lib/ffi/encoding.rb', line 18

def self.for(encoding)
  new(encoding)
end

Instance Method Details

#from_native(value, _ctx) ⇒ Object



22
23
24
# File 'lib/ffi/encoding.rb', line 22

def from_native(value, _ctx)
  value.force_encoding(encoding)
end

#native_type(_type = nil) ⇒ Object



30
31
32
# File 'lib/ffi/encoding.rb', line 30

def native_type(_type = nil)
  FFI::Type::STRING
end

#to_native(value, _ctx) ⇒ Object



26
27
28
# File 'lib/ffi/encoding.rb', line 26

def to_native(value, _ctx)
  value.encode(encoding)
end