How to enable ruby to see tk in the require tk statement? - ruby

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.

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

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.

Related

Load Error when requiring taglib-ruby

I'm trying to use the Ruby wrapper gem for Taglib to play around with ID3 Tags in a practice program. I'm getting Load Errors regarding the requiring of the taglib ruby gem.
I've installed the gem into my project via RubyGems and am simply requiring the gem as stated in a number of posts:
require 'taglib'
These are the software versions I'm working with:
ruby 2.0.0p481
taglib-ruby (0.7.1)
taglib-1.9.1
I'm on a Mac with Mavericks 10.9.5, using RubyMine as my IDE.
I'm not sure if my installation is correct for taglib (the original, not the Ruby wrapper). I used Homebrew to download the .tar.gz file and then unzipped this. The Taglib 1.10 folder is sitting in my local downloads folder - should this be placed somewhere else?
As mentioned, I'm requiring 'taglib' at the top of my .rb file. The error I'm getting when trying to run this file is:
'require': cannot load such file -- taglib (LoadError)
I'm pretty new to Ruby and SO so anything else I need to clarify, please ask. Any help would be appreciated, thanks a lot.
When you require a file, the file must be either in your $LOAD_PATH variable for Ruby, or explicitly indicated in the require string.
The error that you posted is specific to this. The require command leaves off the extension, as it will load other types of files other than Ruby if they are available. See the documentation on the require method for more information.
If you have installed the library in ~/my_projects/music_analyzer/taglib_unzip_folder then you can use require '~/my_projects/music_analyzer/taglib_unsip_folder/taglib and the error messages should change.
If you have installed the gem, and are using rvm, you should not use sudo, but should use gem install taglib-ruby
And in this case you would not need to specify the folder name, as the path would be included in Ruby's load path.
If you are using an IDE, and that IDE is not using the same environment, then you will end up with load problems, as your installation and the IDE's environment may not be identical.
Instead of using the IDE, you can test these things out right at the terminal, using irb.
>> require 'taglib'
=> true
On your Mac Terminal try to type gem list. This should give you the list of installed gem. If the gem is not showing up, type gem install "gem-name" to install it.

Nokogiri Ruby 'require' Issues

I'm new to Ruby and I'm having a lot of trouble trying to use Nokogiri. I've been trying to find a resolution for hours now, so any help is appreciated. I tried searching for and using solutions from other related SO posts before caving and posting my own. When I run ruby -v I get: ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
(Edit: I have updated ruby with updates-alternatives --config ruby and selected /usr/bin/ruby1.9.1 but when I do ruby -v it is now showing version 1.9.3 WTF am I doing wrong here?)
I have a new project directory at ~/workspace/ruby/rubycrawler/ and I used Bundler to install nokogiri, which installed correctly:
Using mini_portile (0.5.2)
Using nokogiri (1.6.1)
Using bundler (1.5.1)
Your bundle is complete!
Running bundle show nokogiri returns /var/lib/gems/1.9.1/gems/nokogiri-1.6.1.
In the directory I'm running the script from I have a simple html file named "index.html". The script I'm trying to run is even simpler (or so I thought):
require 'nokogiri'
page = Nokogiri::HTML(open("index.html"))
puts page.class # Nokogiri::HTML::Document
The error is rubycrawler.rb:1:in 'require': no such file to load -- nokogiri (LoadError).
I also added require 'rubygems' even though I read it isn't needed for 1.9+ and still no luck.
A lot of searching shows "Did you put this gem in your Gemfile?". So I generate a Gemfile and add gem 'nokogiri'. I try running the small script again and get the same error. I read "Try deleting Gemfile.lock." so I did but still couldn't get it to work. I then read to try testing it out in irb so I tested "open-uri" and "nokogiri" and here's what I got:
irb(main):001:0> require 'open-uri'
=> true
irb(main):003:0> require 'nokogiri'
LoadError: no such file to load -- nokogiri
I'm really having a lot of trouble figuring this out, so really any help at all is really appreciated.
Ruby tools like RVM, Bundler, etc., to the novice, appear to do a lot of magic, but really, there is no magic to them. The key here lies in what Bundler actually does for you. It manages a manifest of dependencies, BUT at runtime, those dependencies STILL have to get loaded somehow, and my gut feeling is that is what is not happening here.
Regardless of what version of Ruby you are using, if you are using Bundler, there's an easy way to do this. Precede the command that starts your program with "bundle exec" and that will make Bundler edit Ruby's load path so that it includes all the things in the manifest (Gemfile.lock).
For example:
$ bundle exec ruby foo.rb
A additional note for anyone using RVM: RVM generally will modify the shebangs in the scripts that launch programs like "ruby" or "rake" so that they use the "ruby_no_exec" shell (or similar) instead of the plain old "ruby" shell. That alternate shell is Bundler-aware and makes it generally unnecessary to type "bundle exec," but since the OP is using system Ruby, that's not applicable and commands should be manually prefixed with "bundle exec".
Hope this helps!
In addition to Kent's answer, I would recommend switching to RVM instead of using the system installed ruby. System rubies tend to be horribly out of date, especially when it comes to important things like features and security updates. It might not help you in your current situation, but it would be well worth the time. If you are unfamiliar: http://rvm.io

Ruby Core Dump On Requiring TK

I tried to use Ruby's TK library today, and it failed miserably, spitting out a nice long core dump.
I'm not sure what I could have done wrong, as I double checked that TK was installed, and even went so far as to reinstall it. I also tried two different versions of Ruby, 2.0.0 and 1.9.3.
The code I tried to execute was simply:
require 'tk'
Here's the core dump on pastebin. Hopefully that will help narrow it down.
http://pastebin.com/LqZ7B8kK
My system details are as follows:
OS: Manjaro Linux, x86_64
Ruby: 1.9.3 and 2.0.0
Using RVM
TK installed
I'd rather not use a different GUI library, as the others seem to be less cross-platform.
Finally, I noticed that StackOverflow doesn't have a tk tag. Someone with enough reputation points might want to create that and get the badge for it. :D
unfortunately pastebin seems to be down right now, so I can't help much, have you enabled tk on your rvm installation? apparently it's disabled by default: RVM Ruby with TK installation (OSX)
the relevant code snippet is
rvm install 1.9.2 --enable-shared --enable-pthread --with-tk --with-tcl
for ruby 1.9.2

Ruby not finding rubygems or wx modules

I'm running the default Ruby installation (ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin10]) on my Intel iMac. I updated RubyGems and installed the wxruby gem. I'm trying to run the following sample program:
#!/usr/bin/ruby
require "rubygems"
require "wx"
class MyApp < Wx::App
def on_init
#frame = Wx::Frame.new(nil, -1, "The Bare Minimum")
#frame.show()
end
end
app = MyApp.new()
app.main_loop()
And I get the following error:
==> wxruby-test.rb
/Library/Ruby/Gems/1.8/gems/wxruby-1.9.3-universal-darwin/lib/wxruby2.bundle: dlopen(/Library/Ruby/Gems/1.8/gems/wxruby-1.9.3-universal-darwin/lib/wxruby2.bundle, 9): no suitable image found. Did find: (LoadError)
/Library/Ruby/Gems/1.8/gems/wxruby-1.9.3-universal-darwin/lib/wxruby2.bundle: no matching architecture in universal wrapper - /Library/Ruby/Gems/1.8/gems/wxruby-1.9.3-universal-darwin/lib/wxruby2.bundle
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require'
from /Library/Ruby/Gems/1.8/gems/wxruby-1.9.3-universal-darwin/lib/wx.rb:12
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:60:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:60:in `require'
from wxruby-test.rb:3
If I comment out the require rubygems statement, I get the following error:
==> wxruby-test.rb
wxruby-test.rb:3:in `require': no such file to load -- wx (LoadError)
from wxruby-test.rb:3
I'm new to Ruby on the Mac, and I'm sure this is some basic error probably related to paths, but most explanations about the environment variables are aimed at experienced users. If you need more output from other commands, please let me know. I'm running Ruby from the tcsh shell. I'm sure I'm doing something basic wrong, but I'm just stumped.
If you're running Ruby 1.8.7 you should leave in the require statement:
require "rubygems"
Ruby 1.8 didn't know about gems by default, so we had to tell Ruby to require the gems loader. Ruby 1.9+ bundles it so we no longer have to do the require.
This has nothing to do with the Mac (or Windows or Linux) OS, it's about Ruby's default load paths.
And, as a safety tip, don't mess with Ruby installed by Apple. They installed it for their own use, and it's used for some podcast-creation tools. And, at some future point they might want to add something to the system that takes advantage of an expected configuration of Ruby (or Python or Perl). Changing (or worse, deleting it) can mess you up. So, I recommend you leave it alone and use either rbenv or RVM to install Ruby in a sandbox, where you can poke, prod and mess with it safely.
What version of OS X? If it's one of the Lions, then you're out of luck. The wxwidgets library (and things based on it, like wxruby) is still only 32-bit and based on Carbon. The Lions are 64-bit-only and Carbon is deprecated.

Sinatra doesn't load when running a ruby app

I'm trying to run a Ruby application that requires Sinatra within Ubuntu 10.10. I'm new to the 3 of these technologies so I understand if this question looks dumb to you.
Yesterday I installed ruby doing...
sudo apt-get install ruby1.9.1-full
And sinatra by doing...
sudo gem install sinatra
This is the code I'm trying to run:
require 'rubygems'
require 'sinatra'
get '/' do
"Hi Alex!"
end
When I do ruby1.9.1 -rubygems app.rb nothing happens (Ruby is properly installed since I tried running apps that don't require sinatra and they work OK).
$ ls
app.rb
$
$ ruby1.9.1 app.rb
$
$ ruby1.9.1 -rubygems app.rb
$
I know it should open Sinatra and tell me which port it is listening to.
I've been looking for help through the web and read several of the threads created within this forum but nothing I've tried has worked out for me.
What could be happening here?
Thanks
There was a similar problem with sinatra 1.0 on ruby 1.9.2. The answer there was to add enable :run to your code.
Have a look at the docs for the :run configuration - if you're going to be deploying to a server you'll want to do something like enable :run if __FILE__ == $0 so that you only start the built-in server during development when you need it.
Strictly speaking your code is correct and should run okay, and in fact it does with ruby 1.8.7 and 1.9.2. The problem seems to be running it with ruby 1.9.1. In general 1.9.1 seems to be fairly outdated and you should probably look to upgrade to 1.9.2 if you can. If Ubuntu doesn't have any packages for 1.9.2 take a look at rvm. (In fact if you're going to be doing ruby development rvm is worth a look anyway).
Update:
I managed to get ruby 1.9.1 compiled to test this, and your code worked ok. Also a bit of googling suggests that the Ubuntu ruby1.9.1 package provides ruby 1.9.2 anyway. So there seems to be something else going on causing :run not to be set when running the file directly, though I don't know what that could be.
Another update:
Looking at the Ubuntu Sinatra package it looks like it's at version 1.0. It could be that your setup is using the Ubuntu
package and ignoring the more recent version installed via rubygems. This could explain what's happening. If so this isn't a "a similar problem" to ruby 1.9 and sinatra 1.0 like I suggested above, it's the same problem!
Yet another update:
A couple of things have occurred to me. You can check what version of Sinatra you're actually using with something like puts Sinatra::VERSION after require 'sinatra'. Also, it looks like there is a gem1.9.1 command that corresponds to ruby1.9.1. It looks like when you installed sinatra with sudo gem install sinatra the latest version got installed into the ruby 1.8 install, and left ruby 1.9 with the Ubuntu packaged Sinatra 1.0. If you haven't switched over to rvm yet, you could try sudo gem1.9.1 install sinatra.

Resources