---
title: "FFI Python vs Ruby"
description: "A comparison of Foreign Function Interface (FFI) usage in Python vs Ruby to call C libraries. Practical notes and code in JS."
url: "https://h3manth.com/scribe/ffi-python-vs-ruby/"
canonical_url: "https://h3manth.com/scribe/ffi-python-vs-ruby/"
markdown_url: "https://h3manth.com/scribe/blog/ffi-python-vs-ruby.md"
publishDate: "2012-08-01T00:00:00.000Z"
author: "Hemanth HM"
category: "python"
tags: ["python","ruby"]
word_count: 163
reading_time_minutes: 1
x_markdown_tokens: 357
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# FFI Python vs Ruby

> A comparison of Foreign Function Interface (FFI) usage in Python vs Ruby to call C libraries. Practical notes and code in JS.

Lately wrote about [Python vs Ruby Singleton](http://h3manth.com/content/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:**

```python
>>> 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`):**

```ruby
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.
