Coding / running Ruby in XCode

15,226

Solution 1

Take a look at MacRuby. The current stable version has Xcode integration. It does need a 64-bit system running Snow Leopard.

Solution 2

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

Share:
15,226
AndyNico
Author by

AndyNico

Updated on June 09, 2022

Comments

  • AndyNico
    AndyNico almost 2 years

    I am currently using TextMate for OS X to create basic Ruby code (still learning), saving down as a .rb file 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?