tags:

views:

729

answers:

2

I am starting to learn the very basics of ruby on my iMac running Leopard.

The version that is now on my system is ruby 1.8.6

But, I independently also installed ( via MacPorts ) ruby 1.9 because that corresponds to the latest "PickAxe" book.

The installation location for ruby 1.8.6 is in /usr/bin/ruby The installation location for ruby 1.9.1 is in /opt/local/bin ( installed there when I used MacPorts )

My question has to do with an error message related to the require 'tk' statement in following fairly standard example, which is in the file HelloWorld.rb

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

When I invoke the command ruby HelloWorld.rb I get the results I expect; the program runs.

However, when I invoke the command ruby1.9 HelloWorld.rb I get the error message

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

I have been searching the web and various postings but so far have not been able to find a clear explanation of what I need to do to make ruby1.9 be able to find tk when using the require 'tk' statement.

Does it have to do with running the gem1.9 command ( which is also in /opt/local/bin )?

I have tried invoking sudo gem1.9 install tk but that results in an error message ERROR: could not find gem tk locally or in a repository

Any suggestions would be greatly appreciated.

+1  A: 

You need to compile ruby using the with-tcltk-framework flag in your ./configure call. Full details can be found on the TkDocs - Installing Tk page. The first section is Mac OS X, and then the portion of that with the Ruby logo next to it is just what you need.

Another thing worth noting is the website recommends against using Ruby 1.9.x for Tk. However, the only way to know for sure if it will work is trying it out yourself.

statenjason
+1  A: 

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
hhaamu
Yes, the command: port install ruby19 +mactkworked!
Richard Fuhr