Skip to content

Latest commit

 

History

History
34 lines (23 loc) · 934 Bytes

README.md

File metadata and controls

34 lines (23 loc) · 934 Bytes

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