Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3612432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:59:53+00:00 2026-05-18T21:59:53+00:00

I have a simple class that defines some constants, e.g.: module Foo class Bar

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:59:54+00:00Added an answer on May 18, 2026 at 9:59 pm

    The only way this can happen, is when bar.rb is required multiple times. Which shouldn’t happen, since require doesn’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:

    require 'bar'   # => true, file was loaded
    
    require 'bar'   # => false, file had already been loaded
    
    require './bar' # => true, OOPS, I DID IT AGAIN
    # bar.rb:3: warning: already initialized constant BAZ
    

    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_PATH

      require "File.expand_path('../lib/bar', File.dirname(__FILE__))"
      
    • manipulating $LOAD_PATH anywhere except maybe the main entry point to your library:

      path = File.expand_path(File.dirname(__FILE__))
      $LOAD_PATH << path unless $LOAD_PATH.include?(path)
      

    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 uses setup.rb, then it will be installed in site_ruby, which is already on the $LOAD_PATH anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple class that essentially just holds some values. I have overridden
I have a really simple Java class that effectively decorates a Map with input
I have a really simple class with two methods; One that will be called
I have the simple class using auto-implemented properies: Public Class foo { public foo()
Very simply put: I have a class that consists mostly of static public members,
For example I have a simple class like public class Person{ public int Age
I have a very simple class with only one field member (e.g. String). Is
Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True)
I have a simple database table called Entries: class CreateEntries < ActiveRecord::Migration def self.up
I have a simple GUI component written in Java. The class draws an analog

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.