Ruby mysql gem works but I see an exception when I execute the script with debug option using -d $DEBUG
I have got some CGI scripts. I use mysql connection and queries in these scripts. After seeing some anomalies in the scripts I wanted to execute them using ruby debug options. Here, below, a small test script that shows the problem.
Without debug options, there is nothing, no error, I can connect to database and run queries but when I use debug option ( -d $DEBUG ) it throws an exception. Interesting thing is, even there is exception, it works, it connects to database.
Any idea ? What is the problem ? How can I fix it ? It is fresh Ubuntu 11.04 install. Details are below.
rvm -v:
rvm 1.8.4 by Wayne E. Seguin (wayneeseguin@gmail.com) [https://rvm.beginrescueend.com/]
ruby -v:
rub开发者_Python百科y 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
gem -v:
1.8.10
gem list:
* LOCAL GEMS *
mysql (2.8.1)
mysql2 (0.3.7)
rake (0.9.2)
ruby dbtest.rb:
14701920
ruby -d $DEBUG dbtest.rb:
Exception `LoadError' at /home/mehmet/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36 - no such file to load -- mysql
11340120
more dbtest.rb:
#!/home/paul/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
require 'rubygems'
require 'mysql'
connection = Mysql.new("localhost", "", "", "mymarket")
puts connection.object_id
Cheers,
Rubgems first attempts to load a require
d file through the normal Ruby require
mechanism. Only if this fails, it uses the gem infrastructure.
What you are seeing is the failure (exception) of the non-gem require
. After that, the gem require
catches up and correctly loads the gem.
The global variable $DEBUG
makes Ruby display exceptions even if they are caught.
From the source of custom_require.rb with line 36 highlighted:
alias gem_original_require require
...
def require path
if Gem.unresolved_deps.empty? or Gem.loaded_path? path then
gem_original_require path # <---- line 36
else
...
end
rescue LoadError => load_error
if load_error.message.end_with?(path) and Gem.try_activate(path) then
return gem_original_require(path)
end
raise load_error
end
UPDATE:
For solving this issue, either
- ignore the message (It is harmless.)
- "activate" the gem with
gem 'mysql'
before therequire
(This will setup the search path for the gem to be found by the initial non-gem attempt.)
精彩评论