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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:29:51+00:00 2026-05-23T13:29:51+00:00

I have some configuration options for a program stored in a JSON file. I

  • 0

I have some configuration options for a program stored in a JSON file. I want to be able to access the options in several different classes without explicitly having to open the file and read the configuration in each class. Is there a good, DRY way to do this?

I tried creating a module which reads the configuration into a class variable, and just include the module in every class, but is this a good use of class variables? Reading this makes me wary of class variables.

Here is what I have right now:

module Config
  @@config = nil

  def self.included(base)
    if @@config.nil?
      open('config.json', 'r') { |f| @@config = JSON.load(f) }
    end
  end
end

Thanks!

Update:

Maybe it’s better just to place all the classes that require the configuration under it’s own namespace?

module MyFishTank
  Config = { "location" => "My room" }

  Class Fish
    def location
      Config['location']
    end
  end
end

fish = MyFishTank::Fish.new
puts fish.location #=> "My room"
  • 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-23T13:29:52+00:00Added an answer on May 23, 2026 at 1:29 pm

    Why not something like this:

    module Configured
      def config
        # memoize config upon load
        @config ||= 'foo'
      end
    end
    
    class A
      extend Configured
    
      def initialize
        puts A.config # or
        puts self.class.config
      end
    end
    
    A.new
    #=> foo
    

    Edit: if you want to avoid class method invocation (which seems wrong to me, as the configuration is indeed class-wide, not instance-wide), you can do something like:

    module Configured
      def self.included(base)
        base.define_singleton_method :config do
          @config ||= 'foo'
        end
      end
    
      def config
        self.class.config
      end
    end
    
    class A
      include Configured
    
      def initialize
        puts config
      end
    end
    
    A.new
    

    Though there probably is more succinct way of doing it.

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

Sidebar

Related Questions

I have some configuration data in a config file that I read off disk
I have some values in a configuration file (XML file) with some values like
I have a project that requires some configuration files. I want to keep the
in the configuration file i have entries similar to this one: filepath = c:\Program
I have a program that is using a configuration file. I would like to
I need to have some configuration options on my website. I thought it would
I have some sensitive files that I want the web server to be able
I have some configuration values for an asp.net web app. They will be maintained
I have some configuration data that I'd like to model in code as so:
I am working on a small AIR desktop application and I have some configuration

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.