Class: FFI::Struct
- Inherits:
-
Object
- Object
- FFI::Struct
- Defined in:
- lib/ruby-libstorj/struct.rb
Direct Known Subclasses
LibStorj::Ext::Storj::BridgeOptions, LibStorj::Ext::Storj::Bucket, LibStorj::Ext::Storj::CreateBucketRequest, LibStorj::Ext::Storj::DownloadState, LibStorj::Ext::Storj::EncryptOptions, LibStorj::Ext::Storj::Env, LibStorj::Ext::Storj::File, LibStorj::Ext::Storj::GetBucketRequest, LibStorj::Ext::Storj::HttpOptions, LibStorj::Ext::Storj::JsonRequest, LibStorj::Ext::Storj::ListFilesRequest, LibStorj::Ext::Storj::LogOptions, LibStorj::Ext::Storj::UploadOptions, LibStorj::Ext::Storj::UploadState, LibStorj::Ext::UV::Work
Instance Method Summary collapse
-
#map_layout ⇒ Object
convenience method for inspecting struct hierarchies.
-
#values_at(*members) ⇒ Object
allows for destructuring.
Instance Method Details
#map_layout ⇒ Object
convenience method for inspecting struct hierarchies
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ruby-libstorj/struct.rb', line 18 def map_layout # NB: Hash[ [ [key, value], ... ] ] → new_hash Hash[members.map do |member_name| member = self[member_name] value = member.is_a?(FFI::Struct) ? member.map_layout : member if value.is_a? FFI::Pointer next [member_name, nil] if value.null? # attempt to read as string begin string_value = value.read_string # update value to string_value if it contains only printable characters if string_value =~ /^[[:print:]]*$/ value = string_value end rescue # do nothing... # let value remain a pointer if an exception was raised end end [member_name, value] end] end |
#values_at(*members) ⇒ Object
allows for destructuring. NB: values_at members array order
must match assignment order!
example: “‘
bucket = ::LibStorj::Ext::Bucket.new
name, id, decrypted = bucket.values_at(:name, :id, :decrypted)
# OR name, id, decrypted = bucket.values_at([:name, :id, :decrypted])
# OR name, id, decrypted = bucket.values_at(%i[name id decrypted])
```
13 14 15 |
# File 'lib/ruby-libstorj/struct.rb', line 13 def values_at(*members) members.flatten.map {|member_name| self[member_name]} end |