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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:10:29+00:00 2026-05-27T04:10:29+00:00

I just want to have an array as global, so when I add or

  • 0

I just want to have an array as global, so when I add or remove an element,
it can be reflected anywhere in the class.

For example:

class something
  @@my_array = Array.new
  def self.action_1
    @@my_array << 1
    @@my_array << 2
    @@my_array << 3
  end

   def self.how_many_elements
     puts "# of elements: " + @@my_array.size.to_s
   end
end

If i do the following:

something.action_1 => from controller_a

something.how_many_elements => from controller b

I always get the following output:

"# of elements: 0"

Why?

  • 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-27T04:10:30+00:00Added an answer on May 27, 2026 at 4:10 am

    It’s a common mistake to think that you can stash things in your classes and they will persist between requests. If this happens it is purely coincidence and it is a behavior you cannot depend on.

    Using global variables in this fashion is almost a bad idea. A properly structured Rails application should persist data in the session, the database, or the Rails.cache subsystem.

    Each request serviced by Rails in development mode will start with a virtually clean slate, where all models, controllers, views and routes are reloaded from scratch each time. If you put things in a class thinking it will be there for you the next time around, you’re going to be in for a surprise.

    For saving things that are not important, use the Rails cache or the session facility. For saving things that are important, use a database.

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

Sidebar

Related Questions

I have a class that inherits QMainWindow and I just want it to have
I won't want to have edit any working sets. I just want a way
Hi I just want to make sure I have these concepts right. Overloading in
I have a FormCollection and I just want to only iterate through the keys
I have a File having text and few numbers.I just want to extract numbers
i have a datatable with the results i just want to refine the results
I have written a Perl script, I just want to give it to every
does anyone have any examples of using NSKeyedArchiver in monotouch? i just want to
I have a String with this content: href=http://www.website.com/ /> [...] [...] I just want
Update: Based on a couple of the answers I have received, I just want

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.