Subject:
Re: [ruby-ffi] Re: struct questions
From:
Shawn Anderson
Date:
12/20/09 9:12 AM
To:
ruby-ffi@googlegroups.com

nice-ffi by jacius works for this too.

On Sat, Dec 19, 2009 at 11:34 PM, Wayne Meissner <wmeissner@gmail.com> wrote:
2009/12/20 rogerdpack <rogerpack2005@gmail.com>:
> When I first started using FFI::Struct's
>
> I anticipated being able to call methods, like
> a = Struct.new
> a.name = "hello there"
>
> (since that's how you access them in C, and they are certainly not
> hashes).  Would a patch for such be acceptable?

I considered this at one point, but it leads to name clashes with
other methods you might want to call on a Struct instance - so you
have a namespace issue.

e.g.

class S < FFI::Struct
 layout :size, :int
end

s = S.new

What does s.size return?  The size of the struct, or the contents of
the native field called size?

Using the [] method to access struct fields neatly avoids it.

If you really want it, ffi-swig-generator generates accessor methods
like that for every field, but it should not be a part of ffi itself.