1

我开始在运行 Leopard 的 iMac 上学习 ruby​​ 的基础知识。

我系统上现在的版本是 ruby​​ 1.8.6

但是,我还独立安装了(通过 MacPorts)ruby 1.9,因为它对应于最新的“PickAxe”书。

ruby 1.8.6 的安装位置在 /usr/bin/ruby ruby​​ 1.9.1 的安装位置在 /opt/local/bin (我使用 MacPorts 时安装在那里)

我的问题与以下相当标准的示例中与 require 'tk' 语句相关的错误消息有关,该示例位于文件 HelloWorld.rb 中

require 'tk'
root = TkRoot.new { title "Hello world" }
TkLabel.new(root) do
    text 'Hello world!'
end
Tk.mainloop

当我调用命令 ruby​​ HelloWorld.rb 我得到了我期望的结果;程序运行。

但是,当我调用命令 ruby​​1.9 HelloWorld.rb 我收到错误消息

HelloWorld.rb:1:in require': no such file to load -- tk (LoadError) from HelloWorld.rb:1:in'

我一直在搜索网络和各种帖子,但到目前为止,我还没有找到清楚的解释,说明我需要做什么才能使 ruby​​1.9 在使用 require 'tk' 语句时能够找到 tk。

它是否与运行 gem1.9 命令(也在 /opt/local/bin 中)有关?

我尝试调用 sudo gem1.9 install tk 但这会导致错误消息 ERROR: could not find gem tk local or in a repository

任何建议将不胜感激。

4

2 回答 2

6

The TK bindings for ruby use compiled code (and not pure ruby), so two installations are needed for different ruby versions.

Darwinports isn't building ruby 1.9 with TK support by default. You need to select either the tk or mactk variant (see the portfile)

It's been a while since I've used darwinports, but I think this was the syntax:

port install ruby19 +mactk
于 2009-07-26T19:16:09.560 回答
1

您需要使用调用中的with-tcltk-framework标志编译 ruby​​。./configure完整的详细信息可以在TkDocs - 安装 Tk页面上找到。第一部分是 Mac OS X,然后旁边带有 Ruby 徽标的部分正是您所需要的。

另一件值得注意的事情是该网站建议不要将 Ruby 1.9.x 用于 Tk。但是,确定它是否有效的唯一方法是自己尝试一下。

于 2009-07-21T02:05:26.083 回答