Class: GLib::Array
- Inherits:
-
Object
- Object
- GLib::Array
- Extended by:
- ContainerClassMethods
- Includes:
- Enumerable
- Defined in:
- lib/ffi-glib/array.rb
Overview
Overrides for GArray, GLib’s automatically growing array. It should not be necessary to create objects of this class from Ruby directly.
Instance Attribute Summary collapse
-
#element_type ⇒ Object
readonly
Returns the value of attribute element_type.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #append_vals(ary) ⇒ Object
- #each ⇒ Object
- #get_element_size ⇒ Object (also: #element_size)
-
#index(idx) ⇒ Object
Re-implementation of the g_array_index macro.
-
#initialize(type) ⇒ Array
constructor
A new instance of Array.
- #length ⇒ Object
- #reset_typespec(typespec = nil) ⇒ Object private
Methods included from ContainerClassMethods
Constructor Details
Instance Attribute Details
#element_type ⇒ Object (readonly)
Returns the value of attribute element_type.
15 16 17 |
# File 'lib/ffi-glib/array.rb', line 15 def element_type @element_type end |
Class Method Details
.calculated_element_size(type) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
29 30 31 32 |
# File 'lib/ffi-glib/array.rb', line 29 def self.calculated_element_size(type) ffi_type = GirFFI::TypeMap.type_specification_to_ffi_type(type) FFI.type_size(ffi_type) end |
.from_enumerable(elmtype, arr) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
24 25 26 |
# File 'lib/ffi-glib/array.rb', line 24 def self.from_enumerable(elmtype, arr) new(elmtype).tap { |it| it.append_vals arr } end |
Instance Method Details
#==(other) ⇒ Object
57 58 59 |
# File 'lib/ffi-glib/array.rb', line 57 def ==(other) to_a == other.to_a end |
#append_vals(ary) ⇒ Object
35 36 37 38 39 |
# File 'lib/ffi-glib/array.rb', line 35 def append_vals(ary) bytes = GirFFI::InPointer.from_array element_type, ary Lib.g_array_append_vals(self, bytes, ary.length) self end |
#each ⇒ Object
41 42 43 44 45 |
# File 'lib/ffi-glib/array.rb', line 41 def each length.times do |idx| yield index(idx) end end |
#get_element_size ⇒ Object Also known as: element_size
51 52 53 |
# File 'lib/ffi-glib/array.rb', line 51 def get_element_size Lib.g_array_get_element_size self end |
#index(idx) ⇒ Object
Re-implementation of the g_array_index macro
73 74 75 76 77 78 79 80 81 |
# File 'lib/ffi-glib/array.rb', line 73 def index(idx) unless (0...length).cover? idx raise IndexError, "Index #{idx} outside of bounds 0..#{length - 1}" end item_ptr = data_ptr + idx * element_size convertor = GirFFI::ArrayElementConvertor.new element_type, item_ptr convertor.to_ruby_value end |
#length ⇒ Object
47 48 49 |
# File 'lib/ffi-glib/array.rb', line 47 def length struct[:len] end |
#reset_typespec(typespec = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
62 63 64 65 66 67 68 69 70 |
# File 'lib/ffi-glib/array.rb', line 62 def reset_typespec(typespec = nil) if typespec @element_type = typespec check_element_size_match else @element_type = guess_element_type end self end |