

GitHub - johnfairh/RubyGateway: Embed Ruby in Swift: load Gems, run Ruby scripts...
source link: https://github.com/johnfairh/RubyGateway
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md
RubyGateway
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.
This project is young. Eventually plan to allow implementation of Ruby classes in Swift, enabling Ruby as a DSL/scripting language for Swift applications.
See CRuby if you are looking for a low-level Ruby C API wrapper.
Examples
A couple of examples:
Services
Rouge is a code highlighter. In Ruby:
require 'rouge' html = Rouge.highlight("let a = 3", "swift", "html") puts(html)
In Swift 4.1 with similar [lack of] error checking:
import RubyGateway try! Ruby.require("rouge") let html = try! Ruby.get("Rouge").call("highlight", args: ["let a = 3", "swift", "html"]) print(html)
In future Swift:
import RubyGateway try! Ruby.require("rouge") let html = Ruby.Rouge!.highlight("let a = 3", "swift", "html")! print(html)
Objects
// 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! student.call("add_score", args: [readingSubject, 30]) try! student.call("add_score", args: [readingSubject, 35]) // Get a result as floating point let avgScoreObj = try! student.call("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") scores.call("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)
Documentation
Requirements
- Swift 4.1 or later, from swift.org 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, this comes 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.
Installation
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: "https://github.com/johnfairh/RubyGateway", from: "0.1.0")
CocoaPods for macOS:
use_frameworks!
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 instructions to change this. For example on Linux using Brightbox Ruby 2.5:
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
Contributions
Welcome: open an issue / [email protected]
License
Distributed under the MIT license.
Recommend
-
132
Milksnake Milksnake is an extension for setuptools that allows you to distribute dynamic linked libraries in Python wheels in the most portable way imaginable. It gives you a hook to invoke your own build process and to then take t...
-
10
Making Ruby Scripts work Under System D Using rbenv Feb 13, 2020 This has bit me I think every single time I make a Sy...
-
10
18 Mar 2021 Swift CryptoKit and Ruby/Python I spent days figuring out how to decrypt ChaChaPoly encrypted data with Swift CryptoKit using other languages. What should have taken me minutes took me hours. As a t...
-
7
18 Mar 2021 Updated: 22 Mar 2021 software-dev ...
-
9
How to embed videos in GitHub pages without growing the repository size Posted on July 23, 2021
-
7
ibm-watson-embed-model-builder This python library manages the process of building a collection of docker images that wrap individual watson_embedded models for delivery with an embeddable watson runtime. Overview Given a...
-
14
IBM/ibm-watson-embed-charts main
-
5
english-script Embed natural language in your code. Allow me to completely take out of context this great Djikstra essay
-
9
Swift for Ruby developers crash course Joe Masilotti...
-
5
Workaround: Swift scripts importing Cocoa frameworks broken on macOS 14 22 January 2024 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK