I have a simple class that defines some constants, e.g.:
module Foo
class Bar
BAZ = "bof"
...
Everything is puppies and rainbows until I tell Rake to run all my Test::Unit tests. When it does, I get warnings:
bar.rb:3: warning: already initialized constant BAZ
My habit has been to avoid these warnings by making the constant initialization conditional, e.g.:
...
BAZ = "bof" unless const_defined? :BAZ
...
This seems to solve the problem, but it is a little tedious, and I don’t ever see anyone else initializing constants this way. This makes me think I might be Doing It Wrong. Is there a better way to initialize constants that won’t generate warnings?
Update: By way of a little more detail on how I’m using these constants, let’s say I’ve defined a Token class that has constants for all the characters that are part of the syntax of some artificial language. I also have a Scanner class that reads a stream of characters, generating a Token instance for each one.
module Foo
class Token
LPAREN = "("
RPAREN = ")"
...
end
class Scanner
def next_token
case read_char()
when Token::LPAREN: # Generate a new LPAREN token
...
That is, when checking to see what kind of token should be generated for the given character, I want to use the constants defined in Token.
Update 2: Jörg’s answer revealed that the problem was probably in how I was constructing paths in my require statements, not in how I was initializing or using the constants. I rewrote my require statements to eliminate any manual path creation, e.g.:
# File: $PROJECT_ROOT/lib/foo.rb; trying to load $PROJECT_ROOT/lib/foo/bar.rb
require File.expand_path(File.dirname(__FILE__)) + "foo/bar"
is now written to rely on $LOAD_PATH:
# File: $PROJECT_ROOT/lib/foo.rb; trying to load $PROJECT_ROOT/lib/foo/bar.rb
require 'lib/foo/bar'
I removed the conditional checks from my constant initialization statements, and rake now runs unit tests without throwing any warnings.
The only way this can happen, is when
bar.rbisrequired multiple times. Which shouldn’t happen, sincerequiredoesn’t load files that have already been loaded once.It does, however, only use the path you pass to it to determine whether a file has already been loaded, at least in Ruby 1.8:
So, you are right: this could very well be an indication that there is something wrong with your dependency management.
Typical warning signs are
manually constructing file paths instead of just relying on
$LOAD_PATHmanipulating
$LOAD_PATHanywhere except maybe the main entry point to your library:In general, my philosophy is that it’s not my job as a library writer to figure out how to put my library on the
$LOAD_PATH. It’s the system administrator’s job. If the sysadmin uses RubyGems to install my library, then RubyGems will take care of it, otherwise whatever other package management system he uses should take care of it, and if he usessetup.rb, then it will be installed insite_ruby, which is already on the$LOAD_PATHanyway.