FFI Python vs Ruby

Lately wrote about Python vs Ruby Singelton, this is a similar write up that is crips and talks about FFI in both of them.

FFI foreign function interface :

A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written in another. Despite the name, FFIs are not necessarily restricted to function calls; many FFIs permit method calls on objects; and some even permit migration of non-trivial datatypes and/or objects across the language boundary.

FFI in Python to get the PID using C lib:

>>> import ctypes
>>> libc = ctypes.CDLL("libc.dylib", ctypes.RTLD_GLOBAL) # On GNU/Linux '/lib/libc.so.6'
>>> libc.getpid()
5516

FFI in Ruby to get the PID using C lib needs a gem install ffi:

require 'ffi'
 
module GetPid
  extend FFI::Library
 
  attach_function :getpid, [], :uint
end
 
puts GetPid.getpid

This ends up in a draw? Or you see a clear winner? Well.. Left to your discretion.

Share this