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

  • Home
  • SEARCH
  • 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 6598885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:24:50+00:00 2026-05-25T18:24:50+00:00

I have a module that has multiple class variables. I’m looking for a class

  • 0

I have a module that has multiple class variables. I’m looking for a class level getter implementation that will only instantiate the @@ variable when the class tries to access it like the following

    module MyProducts
      @@products = nil

      def self.get_product(id)
        # i'm looking for a way that the first call on @@products does a find via AR                 like the following
        # @@products = Product.all
        # this module is in the lib directory of a Rails 2.3.5 app
        @@products.find do |prod|
          prod.id.eql?(id)
        end
      end
    end

I’m looking for this to be transparent so that i don’t have to modify the whole module. There are about 10 class level variables with similar functions, all the results of an ActiveRecord .find call

  • 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-25T18:24:51+00:00Added an answer on May 25, 2026 at 6:24 pm

    Just use the ||= operator. It would evaluate the right expression only if the right part is nil or false

    def foo
      @@any ||= some_value
    end
    

    The first time you call the method it will initialize the variable with the result of some_value, and following calls will return the @@any value with no need of recomputing some_value.

    Update

    Here it’s a little script which shows you how to do that. If you execute that you’ll see that the method complex_function is called once since the two print statements both returns 1. However from your comment I see that your Product is an active record, so don’t use this approach for what your asking for, it will be very inefficient (read the last part of my answer)

    #!/usr/bin/env ruby
    
    module Foo
      def self.products
        @@products ||=complex_function
      end
    
      @@a = 0
    
      def self.complex_function
        @@a += 1
      end
    end
    
    p Foo.products
    p Foo.products
    

    Update end

    However your approach to save Product.all is pretty inefficient:

    • First it will fetch all the products in the database which could consume a lot of memory when you have lot of products
    • Second your iteration code will be much slower than the db when you have a lot of products

    Replace your whole method with a Product.find(id) call.

    If your Product model isn’t stored in the db (maybe an ActiveResource) ignore my previous comment.

    You could also take a look to mattr_accessor and this SO question Difference between mattr_accessor and cattr_accessor in ActiveSupport?

    Finally also take a look to this article which explains the above technique called memoization

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

Sidebar

Related Questions

Let's say that I have a module that has a Queue in it. For
I have a python module that defines a number of classes: class A(object): def
I have to build an application using Maven for PHP that has multiple modules.
I have a module that has a function whose prototype is similar to that
I'm new to Maven. I have a multi-module maven 2 project that has the
I have a very large codebase (read: thousands of modules) that has code shared
I have module that implements custom content type via NodeAPI hooks ( hook_insert ,
I have a module that sends an email to a specified email address but
I have a module that reads the StandardError of a process. Everything works fine,
Typically I create a plugin when I have a module that I know I'm

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.