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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:18:14+00:00 2026-05-23T10:18:14+00:00

Although I have declared the FooFactory class as Singleton, its class variable @@foo gets

  • 0

Although I have declared the FooFactory class as “Singleton”, its class variable @@foo gets instantiated every time. Why is this so?

The main singleton class:

require 'singleton'

class FooFactory
  include Singleton
  @@foo = nil

  def get_foo
    print @@foo.nil?.to_s
    @@foo  ||= "I am a string"
    return @@foo
  end
end

The controller code:

class PagesController < ApplicationController
  def home
    @foo = FooFactory.instance.get_foo
  end
end

The view code :

<%= @foo %>

I expect that the print method in the FooFactory should return false after the FooFactory has been instantiated for the first time. But the console keeps on printing true everytime I refresh the pages/home view.

  • 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-23T10:18:14+00:00Added an answer on May 23, 2026 at 10:18 am

    In development mode, classes are reloaded on every request, losing any class state that you may have stuffed into them. This can be changed by looking for this line in development.rb:

    config.cache_classes = false
    

    and changing it to true, which is how it’s usually set in production.rb. The reason for setting it to false is convenience: you can edit your code and hit refresh to see the changes without restarting your server.

    But in Rails it’s not common to put state into classes and expect it to stay between requests, because virtual machines come and go, and threaded VMs may not access the class state in a thread-safe way. There are workarounds for those issues, but usually there’s a better way to do whatever you’re doing.

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

Sidebar

Related Questions

An enum structure declared in its own class is a member variable to the
Although I don't have an iPhone to test this out, my colleague told me
I have a HashMap (although I guess this question applies to other collections) of
Although somewhat related to this question , I have what I think is a
This time I have an error which I have been trying again to figure
I know I need to have (although I don't know why) a GROUP BY
I use Delphi for many years, and although I have now moved on to
I've switched over to a Mac recently and, although things have been going quite
so I'm using LinQ2SQL quite heavily in my current application, and although I have
In Python specifically, how do variables get shared between threads? Although I have used

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.