marciok / SwiftyRuby

Calling Ruby from Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

alt text SwiftyRuby

A proof of concept, on how to call Ruby from Swift, using CRuby package.


main.swift

import CRuby

ruby_setup()
rb_require("./hello_world/hello.rb") // Importing Ruby file

var stringArg = rb_str_new_cstr("Swift") // Creating new Ruby string
var result = rb_funcallv(0, rb_intern("hello"), 1, &(stringArg)) // Calling the function `hello` passing `stringArg`
var str = rb_string_value_cstr(&(result)) // Extracting the returned value as C string

print(String(cString: str))

ruby_cleanup(0)

hello.rb

def hello name
  "Hello #{name} from Ruby!"
end

Special thanks to @neonacho, who gave me some enlightenment

About

Calling Ruby from Swift


Languages

Language:Swift 92.3%Language:Ruby 7.7%