Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7749163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:58:18+00:00 2026-06-01T10:58:18+00:00

Consider the following two FFI structs: class A < FFI::Struct layout :data, :int end

  • 0

Consider the following two FFI structs:

class A < FFI::Struct
layout :data, :int
end 

class B < FFI::Struct
layout :nested, A
end

To instantiate them:

a = A.new
b = B.new

Now when I try to assign a to b.nested like this:

b[:nested] = a

I get the following error:

ArgumentError: put not supported for FFI::StructByValue

It seems FFI doesn’t allow you to assign using the [] syntax if the nested struct is “nested by value”, that is it is not a pointer. If so, how do I then assign a to b.nested?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:58:20+00:00Added an answer on June 1, 2026 at 10:58 am

    When you use FFI to nest it can work like this:

    b = B.new
    b[:nested][:data] = 42
    b[:nested][:data] #=> 42
    

    The FFI “b” object has created its own “a” object; you don’t need to create your own.

    What it looks like you’re trying to do is create your own “a” object then store it:

    a = A.new
    b = B.new
    b[:nested] = a  #=> fails because "a" is a Ruby object, not a nested value
    

    A solution is to store “a” as a pointer:

    require 'ffi'
    
    class A < FFI::Struct
      layout :data, :int
    end
    
    class B < FFI::Struct
      layout :nested, :pointer  # we use a pointer, not a class
    end
    
    a = A.new
    b = B.new
    
    # Set some arbitrary data
    a[:data] = 42
    
    # Set :nested to the pointer to the "a" object
    b[:nested] = a.pointer
    
    # To prove it works, create a new object with the pointer
    c = A.new(b[:nested])
    
    # And prove we can get the arbitrary data    
    puts c[:data]  #=> 42
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following two scenarios: //Data Contract public class MyValue { } Scenario 1:
Consider the following two struct : struct a { int a; }; struct b
Lets consider following two codes First: for (int i=0;i<10000000;i++) { char* tab = new
Consider the following two snippets: Exhibit A : template<typename CalcFuncT> int perform_calc(CalcFuncT&& calcfunc) {
Consider the following struct: public struct vip { string email; string name; int category;
Consider the following two segments of code in Java, Integer x=new Integer(100); Integer y=x;
The problem is best explained by example, consider the following two models: class Topping(models.Model):
Consider the following two Java classes: a.) class Test { void foo(Object foobar) {
Consider the following two methods, written in pseudo code, that fetches a complex data
Consider the following two classes public class ServerDAOHb implements Serializable { .... @OneToMany(cascade =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.