Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Monday, February 8, 2010

Resolved: "uninitialized constant DBI::DBD::ODBC" in Ruby for Windows

Running:
  • Windows 2003 Server SP2
  • Ruby 1.8.6 patchlevel 383, i386-mingw32 (from RubyInstaller)
  • dbd-odbc 0.2.4
  • dbi 0.4.3
Problem:
I created a Ruby class that can run arbitrary SQL queries on remote databases of various kinds. It was working fine for a while, but then something changed so that whenever I would try to use it to execute some SQL, I'd get the following error:

DBI::InterfaceError (Unable to load driver 'ODBC' (underlying error: uninitialized constant DBI::DBD::ODBC)):
dbi (0.4.3) lib/dbi.rb:300:in `load_driver'
C:/Ruby/lib/ruby/1.8/monitor.rb:242:in `synchronize'
dbi (0.4.3) lib/dbi.rb:242:in `load_driver'
dbi (0.4.3) lib/dbi.rb:160:in `_get_full_driver'
dbi (0.4.3) lib/dbi.rb:145:in `connect'
...

Resolution:
Make sure you have the dbd-odbc and dbi gems installed correctly.
For me, a couple of associated files were missing from C:\Ruby\lib\ruby\1.8\i386-mingw32: odbc.so and odbc_utr8.so. I had these files in my working Dev installation, so I just copied them from Dev to Prod, and it fixed the problem.

Commentary:
This comes from DBI trying to do a require dbd/odbc and not finding that file. When I googled around about the error, mostly people said to make sure you had the dbd-odbc gem installed, which I already did. I tried reinstalling the gem, as well as the dbi gem. This had no effect. The problem was happening on Prod, but I had the same thing set up on Dev, which was working. So, I checked to see what paths require uses like so:

C:\...>irb
irb(main):001:0> $LOAD_PATH
=> ["C:/Ruby/lib/ruby/site_ruby/1.8", "C:/Ruby/lib/ruby/site_ruby/1.8/i386-msvcr
t", "C:/Ruby/lib/ruby/site_ruby", "C:/Ruby/lib/ruby/1.8", "C:/Ruby/lib/ruby/1.8/
i386-mingw32", "."]

I checked through those directories to see what .rb files looked like ODBC-related things that were in Dev but not in Prod. That's how I found those two files. After I copied them to Prod, I tested it in IRB like so:

irb(main):004:0> require 'odbc'
=> true

After that, everything worked fine again. I hope this anecdote can help you! Good luck!

Wednesday, December 30, 2009

Resolved: "failed to build gem native extension" on Windows

Running:
- Windows XP SP3
- Ruby 1.8.6 (patchlevel 383, i386-minw32)

Problem:
When trying to do "gem install" on certain gems, I got a message "failed to build gem native extension" and the installation would fail. Some common gems that this happens to include ruby-debug, ruby-debug-ide, hpricot, sqlite3-ruby, and ruby-oci8.

Resolution:
Install the Ruby Development Kit from RubyForge. It's currently only available as a .7z file, so you'll need 7Zip to open it. Follow the instructions in the included INSTALL.txt file, but make sure when you copy the bin folder to your Ruby installation that you copy just the files inside it. Don't overwrite your Ruby installation's bin folder. That would be very bad.

Notes:
So what is a native extension and why is trying to install this?
Your Ruby interpreter (the ruby.exe program) is written C (or Java if it's jruby). Certain gems need to actually add C libraries to your Ruby installation in order to function properly, not just ruby-language files. They're usually downloaded as source (not binaries) and compiled on your machine to match your system's architecture. The problem is when, for instance, your machine doesn't have a C compiler available or not the same one as the gem is expecting (you may see something about "MSC version unmatch" if the version is different). The Ruby Dev Kit comes with a C compiler (gcc) that runs on Windows and is (probably) compatible with the gems. This resolved my issue.

The fact that you have to download the Dev Kit for building native extensions is apparently a recent change (Nov. '09), and it probably only applies to Windows Ruby installations. This change is currently not well documented, which is why I'm posting my findings.

This can also manifest itself in NetBeans IDE if you opt to install the FastDebugger and get similar error messages about native extensions failing to build.

Related Links:

Tuesday, September 8, 2009

NoMethodError, undefined method `call' for MyController:Class

If you're getting this error:
NoMethodError in MyController#my_action
undefined method `call' for ClientScriptsController:Class

...then you might have forgotten to make your controller inherit (extend) the ApplicationController class, like I did. The big clue is that the undefined method is in the class, not the instance.

Controllers should be set up like this:

class MyController < ApplicationController
def my_action
#do something...
end
end

Friday, August 28, 2009

booleans in ruby


Here's an interesting tidbit about the Ruby language: it has no native boolean type. The true and false keywords are actually instantiations of the TrueClass and FalseClass types. In ruby, things evaluates as a boolean like so:

- false and nil are false

- everything else is true

Sounds intuitive, but it may trip up programmers used to C-style languages to know that 0 (zero) evaluates to true. Here are some tests:





Tests run using Ruby 1.8.6.

Check out this Wikipedia article for more details about booleans in Ruby.