Forum: Ruby on Rails Frustrating "require" problems with RMagick

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B15edcdfdbecf5723067df3e24e4f6f0?d=identicon&s=25 Adam Block (adamblock)
on 2006-12-23 09:17
I am using RMagick in my app and am having problems in deployment. The
deployment machine (Fedora Core 5) can't find RMagick unless I change
the "require" syntax from that used on the dev machine (OS X). And the
dev machine doesn't like the syntax that works on the deployment server!
To wit:

[OS X]
$ script/console
Loading development environment.
>> require_gem 'rmagick'
=> true
>> Magick
=> Magick

$ script/console
>> require 'rmagick'
=> false
>> Magick
=> Magick

$ script/console
Loading development environment.
Application strings file 'config/app_strings.dat' does not exist.
>> require 'RMagick'
[DELETED: Many error of the type:
/usr/local/lib/ruby/site_ruby/1.8/RMagick.rb:32: warning: already
initialized constant PercentGeometry]
NameError: undefined method `assoc' for class `Magick::ImageList'

[Fedora Core 5]
# script/console
>> require_gem 'rmagick'
=> true
>> Magick
NameError: uninitialized constant Magick
        from
/usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in
`const_missing'
        from
/usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in
`const_missing'
        from (irb):2

# script/console
>> require 'rmagick'
MissingSourceFile: no such file to load -- rmagick

# script/console
>> require 'RMagick'
=> true
>> Magick
=> Magick

The gem and the library are installed on both machines (maybe not
entirely correctly, it appears, but they compiled and installed).

To summarize what's happening:
1. require_gem returns true on both machines but on FC5 the library
isn't really loading: Magick isn't initialized.
2. On OS X, "require 'rmagick'" works fine, but FC5 can't find the
library when I spell it in all lowercase.
3. On FC5, "require 'RMagick'" works fine, but on OS X using the
capitalized letters load a broken version of the library.

I've have recompiled and reinstalled till the cows come home. Can anyone
give me some guidance on how to set things up so that one of these three
load commands works consistently on both platforms?

Thanks!

/afb
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-12-23 19:31
(Received via mailing list)
Adam Block wrote:

> [OS X]
> ...
>>> require 'RMagick'
> [DELETED: Many error of the type:
> /usr/local/lib/ruby/site_ruby/1.8/RMagick.rb:32: warning: already
> initialized constant PercentGeometry]

Try deleting the non-gem site_ruby installation of RMagick on OSX.

And then use:

   require_gem 'rmagick'
   require 'RMagick'

--
We develop, watch us RoR, in numbers too big to ignore.
B15edcdfdbecf5723067df3e24e4f6f0?d=identicon&s=25 Adam Block (adamblock)
on 2006-12-23 21:33
 Mark Reginald James wrote:
> Try deleting the non-gem site_ruby installation of RMagick on OSX.
>
> And then use:
>
>    require_gem 'rmagick'
>    require 'RMagick'

I find I don't need to require_gem 'rmagick' on OSX...it's somehow being
loaded when the Rails environment loads:

>> require_gem "rmagick"
=> false

And removing:
- /usr/local/lib/ruby/site_ruby/1.8/RMagick.rb
- /usr/local/lib/ruby/site_ruby/1.8/i686-darwin8.7.3/RMagick.bundle

Doesn't make the following work, unfortunately:

>> require 'RMagick'
[DELETED: Many error of the type:
/usr/local/lib/ruby/site_ruby/1.8/RMagick.rb:32: warning: already
initialized constant PercentGeometry]
NameError: undefined method `assoc' for class `Magick::ImageList'

My best guess is that there is actually something wrong with the gem
build on FC5. I've tried every combination of recompilation so I'm sort
of stuck. ImageMagick command line apps work fine and as far as I can
tell all the libraries are there and the library paths are set in
/etc/ld.so.conf.

Thanks for your suggestions!

/afb
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-12-24 05:50
(Received via mailing list)
Adam Block wrote:

> My best guess is that there is actually something wrong with the gem
> build on FC5. I've tried every combination of recompilation so I'm sort
> of stuck. ImageMagick command line apps work fine and as far as I can
> tell all the libraries are there and the library paths are set in
> /etc/ld.so.conf.

The gem build works fine for me on FC6, but I do need to use
the double-barreled require

    require_gem 'rmagick'
    require 'RMagick'

which is not what the docs say you need to do.

Perhaps update to the latest ImageMagick and ImageMagick-devel
packages, or build ImageMagick from source.

--
We develop, watch us RoR, in numbers too big to ignore.
B15edcdfdbecf5723067df3e24e4f6f0?d=identicon&s=25 Adam Block (adamblock)
on 2006-12-24 06:35
>     require_gem 'rmagick'
>     require 'RMagick'
>
> which is not what the docs say you need to do.

And that *does* work on my FC5 machine. But a capitalized require
'RMagick' makes the Mac barf and it isn't worth hassling with anymore. I
worked around it by catching any LoadError on the lowercase require and
then loading the capitalized one if necessary. Not nice, but I've wasted
enough time on this.

Re: your last three suggestions, done, done, and done. Really it all
installs and compiles beautifully. I'm starting to think the Mac is the
odd one out but I guess we'll never know. :)

Thanks again for your help.

/afb
B33ea5c12d767bfd1253940a960274f5?d=identicon&s=25 Tim Hunter (timhunter)
on 2006-12-27 22:47
Adam Block wrote:
>>     require_gem 'rmagick'
>>     require 'RMagick'
>>
>> which is not what the docs say you need to do.
>
> And that *does* work on my FC5 machine. But a capitalized require
> 'RMagick' makes the Mac barf and it isn't worth hassling with anymore. I
> worked around it by catching any LoadError on the lowercase require and
> then loading the capitalized one if necessary. Not nice, but I've wasted
> enough time on this.
>
> Re: your last three suggestions, done, done, and done. Really it all
> installs and compiles beautifully. I'm starting to think the Mac is the
> odd one out but I guess we'll never know. :)
>
> Thanks again for your help.
>
> /afb

All these replies and nobody has mentioned that "require_gem" doesn't do
what you think it does and isn't necessary anyway?

"require_gem" does not replace "require" for gems. It is used only to
specify a specific version of a gem, which must then be required anyway.
In the current beta version (0.9.0.8) of RubyGems you get a warning
message when you use "require_gem".

The name of the library is "RMagick" and that is what you must specify
in the require statement. This is what all the RMagick examples and the
RMagick doc use:

require "RMagick"

Since OS X is case-insensitive "rmagick" works as well but Linux is
case-sensitive so you must use "RMagick".

Since you've installed RMagick as a gem you must have RubyGems loaded
before requiring RMagick. If you haven't already loaded RubyGems you'll
need that, too.

require "rubygems"
require "RMagick"

But RoR always loads RubyGems so you shouldn't need to do that. A simple
require "RMagick" is all you need. If that doesn't work then RMagick is
not installed properly.

You already know this but I'll say it anyway since so many people are
confused about it: When require fails it raises an exception. When it
returns false it means that whatever you required has already been
loaded.

There are many good tutorials about installing RMagick on OS X on the
web. Google will turn them up quickly. Mine is at
http://rmagick.rubyforge.org/install-osx.html. If you continue to have
problems  try reproducing them in a stand-alone script, outside of your
Rails app. Often this reveals diagnostics that help resolve the problem.
You can always report problems with RMagick by emailing rmagick AT
rubyforge DOT org or post to RMagick's Help forum on RubyForge.

Good luck!
Aa1126cd36ac8570684b7923cff2a6c1?d=identicon&s=25 Gábor Sebestyén (Guest)
on 2006-12-27 23:16
(Received via mailing list)
The trick is that HFS+ filesystem of Mac OS X is case-insensitive by
default. So "RMagick" and "rmagick" is the same on osx. But it's not
the case in other case-sensitive filesystems.

I had the completely the same issue deploying my RoR app to Debian. So
be careful with names in require.

Gábor
B15edcdfdbecf5723067df3e24e4f6f0?d=identicon&s=25 Adam Block (Guest)
on 2006-12-28 01:12
Gábor Sebestyén wrote:
> The trick is that HFS+ filesystem of Mac OS X is case-insensitive...

Interesting. But then why oh why does this happen:

$ script/console
>> require 'rmagick'
=> false
>> Magick
=> Magick

$ script/console
>> require 'RMagick'
[DELETED: Many error of the type:
/usr/local/lib/ruby/site_ruby/1.8/RMagick.rb:32: warning: already
initialized constant PercentGeometry]
NameError: undefined method `assoc' for class `Magick::ImageList'

Obviously the two different case versions are loading different
RMagicks, yeah?

Thanks!

/afb
B33ea5c12d767bfd1253940a960274f5?d=identicon&s=25 Tim Hunter (timhunter)
on 2006-12-28 01:56
Adam Block wrote:
> Gábor Sebestyén wrote:
>> The trick is that HFS+ filesystem of Mac OS X is case-insensitive...
>
> Interesting. But then why oh why does this happen:
>
> $ script/console
>>> require 'rmagick'
> => false
>>> Magick
> => Magick
>
> $ script/console
>>> require 'RMagick'
> [DELETED: Many error of the type:
> /usr/local/lib/ruby/site_ruby/1.8/RMagick.rb:32: warning: already
> initialized constant PercentGeometry]
> NameError: undefined method `assoc' for class `Magick::ImageList'
>
> Obviously the two different case versions are loading different
> RMagicks, yeah?
>
> Thanks!
>
> /afb

In the first case,  "rmagick" has already been required using the
lowercase name, so require knows that it's already been loaded and
returns false. However, require doesn't know that "rmagick" and
"RMagick" are the same thing, so it loads "RMagick" again. The messages
you see are the result of loading RMagick.rb twice. There's no evidence
here that you've loaded two different versions of RMagick.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2006-12-28 02:00
(Received via mailing list)
On Dec 27, 2006, at 7:12 PM, Adam Block wrote:
>
> Thanks!
>
> /afb

Or the simple way that require tracks the already-loaded files makes
the names 'rmagick' and 'RMagick' different (since Ruby Strings are
case-sensitive regardless of the file system).  See the footnote on p.
124 of the pickaxe (Programming Ruby, 2nd ed.) for more including the
bit "Some consider this a bug, and this behavior may well change in
later releases."

-Rob

Rob Biedenharn		http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
B15edcdfdbecf5723067df3e24e4f6f0?d=identicon&s=25 Adam Block (Guest)
on 2006-12-28 02:05
Rob Biedenharn wrote:

> "Some consider this a bug, and this behavior may well change in
> later releases."

Yes, I remember that footnote. :) But it does reraise a question that I
thought of a few nights ago: Is there a way to determine the full paths
of the loaded libraries, or is $: all I get?

Thanks,

/afb
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-12-28 02:20
(Received via mailing list)
On Dec 27, 2006, at 5:05 PM, Adam Block wrote:

> Thanks,
>
> /afb


p $LOADED_FEATURES


cheers-
-- Ezra Zygmuntowicz
-- Lead Rails Evangelist
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
This topic is locked and can not be replied to.