在 XCode 中编码/运行 Ruby

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6015182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:02:17  来源:igfitidea点击:

Coding / running Ruby in XCode

rubyxcodemacos

提问by AndyNico

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.

我目前正在使用 OS X 的 TextMate 创建基本的 Ruby 代码(仍在学习),保存为 .rb 文件并使用终端运行基本代码。

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?

我只是想知道我是否有可能使用 XCode 来完成这项任务?我假设不是根据我一直在阅读的内容,但如果是的话,有人知道如何设置它的指南吗?

采纳答案by Ferruccio

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

看看MacRuby。当前的稳定版本具有 Xcode 集成。它确实需要一个运行 Snow Leopard 的 64 位系统。

回答by sdc

If you want to execute a simple ruby script:

如果你想执行一个简单的 ruby​​ 脚本:

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.rbfile resource copied as a build resource - Warning: The Copy Bundle Resources build phase contains this target's Info.plist file

确保您将my_script.rb文件资源复制为构建资源 - 警告:复制捆绑资源构建阶段包含此目标的 Info.plist 文件