Coding / running Ruby in XCode
I am currently using TextMate for OS X to create basic Ruby code (still learning), saving down as a .rb fi开发者_运维知识库le and using Terminal to run the basic code.
I was just wondering if its possible for me to use XCode for this task at all? I am assuming not from what I've been reading but if it is does anyone know of a guide of how to set this up?
Take a look at MacRuby. The current stable version has Xcode integration. It does need a 64-bit system running Snow Leopard.
If you want to execute a simple ruby script:
if let file = Bundle.main.url(forResource: "my_script", withExtension: "rb") {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = ["ruby", file.path, "arg1"]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data:Data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = String(data: data, encoding: String.Encoding.utf8) {
print(output)
}
}
make sure you have the my_script.rb
file resource copied as a build resource - Warning: The Copy Bundle Resources build phase contains this target's Info.plist file
精彩评论