Skip to content

Instantly share code, notes, and snippets.

@t-cyrill
Last active January 29, 2016 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-cyrill/8a8b1bc1f8b5be40d5a7 to your computer and use it in GitHub Desktop.
Save t-cyrill/8a8b1bc1f8b5be40d5a7 to your computer and use it in GitHub Desktop.
Ruby ffi examples
require 'ffi'
module LibC
extend FFI::Library
ffi_lib FFI::Library::LIBC
typedef :pointer, :FILE
attach_function :fopen, [:string, :string], :FILE
attach_function :fclose, [:FILE], :int
attach_function :fputs, [:string, :FILE], :int
end
handle = LibC::fopen '/tmp/hogehoge', 'w'
LibC::fputs 'hogehoge', handle
LibC::fclose handle
source "https://rubygems.org"
gem 'ffi'
require 'ffi'
module LibC # <--- 1
extend FFI::Library # <--- 1
ffi_lib FFI::Library::LIBC
attach_function :puts, [ :string ], :int
end
LibC::puts 'Hello World!!'
# Mp4v2 binding module
module Mp4v2
module Native
# Mp4v2 Tag struct
class MP4Tags < FFI::Struct
layout :__handle, :pointer,
:name, :string,
:artist, :string,
:album_artist, :string,
:album, :string,
:grouping, :string,
:composer, :string,
:comments, :string,
:genre, :string
def self.keys
[
:name, :artist, :album_artist, :album,
:grouping, :composer, :comments, :genre
]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment