Skip to content

Instantly share code, notes, and snippets.

@lian
Created November 23, 2011 02:20
Show Gist options
  • Save lian/1387734 to your computer and use it in GitHub Desktop.
Save lian/1387734 to your computer and use it in GitHub Desktop.
require 'ffi'
class FFI::Struct
def method_missing(m=nil, *a)
m ? self[m] : super(m, *a)
end
def self.array(size)
klass = self
Class.new(FFI::Struct){ layout :array, [klass, size]; def [](idx); super(:array)[idx]; end; }.new
end
end
class Foo < FFI::Struct
layout :baz, :uint, :bar, :uint
end
f = Foo.array(10)
p f[0].bar
p f[1].baz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment