Platformsosx 10.13

Carthage compatible

Embed Ruby in Swift: load Gems, run Ruby scripts, get results.

RubyGateway is a framework built on the Ruby C API that lets Swift programs
running on macOS or Linux painlessly and safely run and interact with Ruby
programs. It’s easy to pass Swift datatypes into Ruby and turn Ruby objects
back into Swift types.

The current version of the framework entirely covers calling Ruby from Swift,
including passing Swift closures as blocks. The main
features to add cover
implementing Ruby classes and methods in Swift.

See CRuby if you are looking for a
low-level Ruby C API wrapper.



Rouge is a code highlighter. In Ruby:

require 'rouge'
html = Rouge.highlight("let a = 3", "swift", "html")

In Swift 4.1 with similar [lack of] error checking:

import RubyGateway

try! Ruby.require(filename: "rouge")
let html = try! Ruby.get("Rouge").call("highlight", args: ["let a = 3", "swift", "html"])

In future Swift, maybe:

import RubyGateway

try! Ruby.require(filename: "rouge")
let html = try! Ruby.Rouge!.highlight("let a = 3", "swift", "html")


// Create an object.  Use keyword arguments with initializer
let student = RbObject(ofClass: "Academy::Student", kwArgs: ["name": "barney"])!

// Acess an attribute
print("Name is (student.get("name"))")

// Fix their name by poking an ivar
try! student.setInstanceVar("@name", newValue: "Barney")

// Get a Swift version of `:reading`
let readingSubject = RbSymbol("reading")

// Call a method with mixed Swift data types
try!"add_score", args: [readingSubject, 30])
try!"add_score", args: [readingSubject, 35])

// Get a result as floating point
let avgScoreObj = try!"mean_score_for_subject", args: [readingSubject])
let avgScore = Double(avgScoreObj)!
print("Mean score is (avgScore)")

// Pass Swift code as a block
let scores = try! student.get("all_scores")"each") { args in
    print("Subject: (args[0]) Score: (args[1])")
    return .nilObject

// Convert to a Swift array
let subjects = Array<String>(try! student.get("all_subjects"))
subjectsPopularityDb.submit(subjects: subjects)



  • Swift 4.1 or later, from or Xcode 9.3+.
  • macOS (tested on 10.13.3) or Linux (tested on Ubuntu Xenial/16.04 on x86_64) with Clang 6+.
  • Ruby 2.2 or later including development files:
    • For macOS, these come with Xcode.
    • For Linux you may need to install a -dev package depending on how your Ruby
      is installed.
    • RubyGateway requires ‘original’ MRI/CRuby Ruby – no JRuby/Rubinius/etc.


For macOS, if you are happy to use the system Ruby then you just need to include
the RubyGateway framework as a dependency. If you are building on Linux or want
to use a different Ruby then you also need to configure CRuby.

Getting the framework

Carthage for macOS:

github "johnfairh/RubyGateway"

Swift package manager for macOS or Linux:

.package(url: "", from: "1.0.0")

CocoaPods for macOS:

pod 'RubyGateway'

Configuring CRuby

CRuby is the glue between RubyGateway and your Ruby installation. It is a
separate github project but RubyGateway
includes it as submodule so you do not install or require it separately.

By default it points to the macOS system Ruby. Follow the CRuby usage
to change
this. For example on Linux using Brightbox Ruby

sudo apt-get install ruby2.5 ruby2.5-dev pkg-config
mkdir MyProject
swift package init --type executable
vi Package.swift
# add RubyGateway as a package dependency (NOT CRuby)
# add RubyGateway as a target dependency
echo "import RubyGateway; print(Ruby.versionDescription)" > Sources/MyProject/main.swift
swift package update
swift package edit CRuby
Packages/CRuby/cfg-cruby --mode pkg-config --name ruby-2.5
PKG_CONFIG_PATH=$(pwd)/Packages/CRuby:$PKG_CONFIG_PATH swift run


Welcome: open an issue / [email protected]


Distributed under the MIT license.

Latest podspec

    "name": "RubyGateway",
    "version": "1.0.0",
    "authors": {
        "John Fairhurst": "[email protected]"
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "homepage": "",
    "source": {
        "git": "",
        "tag": "v1.0.0",
        "submodules": true
    "summary": "Embed Ruby in Swift: load Gems, run scripts, get results.",
    "description": "A Swift framework built on the Ruby C API that lets Swiftnprograms painlessly and safely run and interact with Rubynprograms.  Easily pass Swift datatypes into Ruby and turnnRuby objects back into Swift types.",
    "documentation_url": "",
    "source_files": [
    "platforms": {
        "osx": "10.13"
    "swift_version": "4.1",
    "frameworks": "Foundation",
    "preserve_paths": [
    "pod_target_xcconfig": {
        "SWIFT_INCLUDE_PATHS": ""${PODS_ROOT}/RubyGateway/CRuby" "${PODS_ROOT}/RubyGateway/RubyGatewayHelpers"",
        "HEADER_SEARCH_PATHS": ""${PODS_ROOT}/RubyGateway/CRuby""
    "prepare_command": "mkdir RubyGatewayHelpersnecho 'module RubyGatewayHelpers [system] {}' > RubyGatewayHelpers/module.modulemap"

Pin It on Pinterest

Share This