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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:14:45+00:00 2026-05-15T08:14:45+00:00

I tried to assign a class static variable like this class QueryLogger < Logger

  • 0

I tried to assign a class static variable like this

class QueryLogger < Logger
  @@query_logger_default_instance = nil
  def self.default_instance
    # Use global variable because static variable doesn't work
    @@query_logger_default_instance ||= self.new(STDOUT)
  end
end

In initializers folder of my rails app, I added a file with this code block

ActiveRecord::Base.logger = QueryLogger.default_instance

In a request (action of controller), I make a call to this: QueryLogger.default_instance.

My assumption is that the call to default_instance will always report the same. However, it does not.

Now I try to watch stuff in NetBeans by setting breakpoint inside default_instance.
Thing happen as expected, the default_instance get called twice, one due to the initializer block and one due to the call to my action. Surprising thing is, in both times, @@query_logger_default_instance report nil inside NetBeans inspector. The first nil report is correct, but the second shocked me. It’s look like static variable gets reset after rails app initialized. Is there some magic there?

  • 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-15T08:14:46+00:00Added an answer on May 15, 2026 at 8:14 am

    It depends if you are running in development environment, classes are loaded per request basis, thats the reason you can make changes to them and see them without restarting the server.

    So i think your class variable will be reset for each request.

    Try running in production.

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

Sidebar

Related Questions

In this C code i have tried to assign pointer addresses of one variable
I tried $this-> but could not assign a value to $first_name and $last_name variable.
I'd like to assign anonymous functions to arrays in PHP inside of a class,
I added the reference to the static ROLES class below to assign the role
currently I use constants for things like *task_type*: class TaskType TWITTER_MENTION_REPLY = twitter_mention_reply TWITTER_FOLLOW
I tried to assign a custom Attribute to class that comes from a dynamic
I am trying to dynamically assign a class variable a value from inside of
I have this Person class that is part of various other transactions like a
Tried to search for this online and didn't get very far, so I'm asking
Tried following the instructions here: How to use Google app engine with my own

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.