Last active
January 29, 2016 16:59
-
-
Save t-cyrill/8a8b1bc1f8b5be40d5a7 to your computer and use it in GitHub Desktop.
Ruby ffi examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem 'ffi' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ffi' | |
module LibC # <--- 1 | |
extend FFI::Library # <--- 1 | |
ffi_lib FFI::Library::LIBC | |
attach_function :puts, [ :string ], :int | |
end | |
LibC::puts 'Hello World!!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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