Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coding / running Ruby in XCode

Tags:

xcode

macos

ruby

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?

like image 300
AndyNico Avatar asked Oct 12 '22 01:10

AndyNico


2 Answers

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

like image 51
Ferruccio Avatar answered Oct 15 '22 10:10

Ferruccio


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

like image 35
sdc Avatar answered Oct 15 '22 11:10

sdc