Lately wrote about Python vs Ruby Singleton, this is a similar write up that is crisp 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 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.
About Hemanth HM
Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.