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 6782475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:44:48+00:00 2026-05-26T16:44:48+00:00

redefining some constant in Ruby (ex. FOO = ‘bar’ ) generates the warning already

  • 0

redefining some constant in Ruby (ex. FOO = 'bar') generates the warning already initialized constant.

I’m trying to write a sort of ReallyConstants module, where this code should have this behaviour:

ReallyConstants.define_constant(:FOO, 'bar') #=> sets the constant ReallyConstants::FOO to 'bar'
ReallyConstants::FOO #=> 'bar'
ReallyConstants::FOO = 'foobar' #=> this should raise an Exception

that is, constant redefinition should generate an exception.

Is that possible?

  • 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-26T16:44:48+00:00Added an answer on May 26, 2026 at 4:44 pm

    Just scope your constant within a module and then use Module#freeze to prohibit further modification of the module.

    E.g.

    module Really
      CONSTANT = :foo
      freeze
    end
    
    Really::CONSTANT = :bar
    -> RuntimeError: can't modify frozen Module
    

    Note that this doesn’t speak to the mutability of the value assigned to the constant. For that, use Object#freeze.

    This approach will bite you hard in environments where your code is reloaded, like in a Rails application. There, you’ll have to jump through an additional hoop, checking whether the module has been defined yet before defining it.

    Generally, defensive programming in Ruby is more trouble than it’s worth. What’s your concern?

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

Sidebar

Related Questions

So, I am writing some sort of a statistics program (actually I am redesigning
we at our company are trying out some new technologies so thought of redesigning
Look at this makefile, it has some sort of primitive progress indication (could have
In Apple's The Objective-C Programming Language: Defining a Class the section named Redefining self
I'm re-defining a method in an object in ruby and I need the new
I would like to be able to alter the size of some of my
I'm currently redesigning a website and have run into an issue with some elements
I recently looked into some .NET memory leaks (i.e. unexpected, lingering GC rooted objects)
In order to refactor some methods into smaller ones, I need pass by reference,
I am preparing some custom performance tests against a legacy application that outputs nonstandard

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.