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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:52:25+00:00 2026-06-14T22:52:25+00:00

Consider: many instances of an object that generates data. It would be great to

  • 0

Consider: many instances of an object that generates data. It would be great to only generate that data once per run.

class HighOfNPeriods < Indicator
  def generate_data
    @indicator_data = DataStream.new
    (0..@source_data.data.count - 1).each do |i|
      if i < @params[:n_days]
      ...
      @indicator_data.add one_data
    end
  end

There are different instances of HighOfNPeriods with different params and different @source_data.

Here is how the indicator is used:

class Strategy
  attr_accessor :indicators

  def initialize params
    ...
  end

The method HighOfNPeriods.generate_data is called from within Strategy. Each Strategy gets a new instance of HighOfNPeriods, so it’s not possible to pull it out as some kind of global value. Besides that, it should not be global.

unless @indicator_data wouldn’t work because the data needs to be shared across many instances of HighOfNPeriods.

So, the question is:

What is a good way to memoize the instance variable `indicator_data` 
or the object `HighOfNPeriods` when there are many different instances, 
some of which have different data?

One solution is to store the data using ActiveRecord, but that’s not really the way I want to do it at this time because:

  1. Not all the generated data can be generated in advance because there are
    too many permutations of params. It makes more sense to see if it has
    been generated before and then generate (and save) as necessary.
  2. It doesn’t take long to generate the data. It may be generated once and used
    hundreds of times each run.
  3. It will be faster to access the data from within the object than to pull it from the database.

Ruby 1.9.3

  • 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-06-14T22:52:27+00:00Added an answer on June 14, 2026 at 10:52 pm

    Make a class variable @@indicator_data that is a hash with [@params,@source_data] as the key and the @indicator_data as the value. Then, at creation, do a memoization on @@indicator_data[[@params,@source_data]].

    class HighOfNPeriods < Indicator
      @@indicator_data = {}
      def generate_data
        @indicator_data = @@indicator_data[[@params, @source_data]] ||= DataStream.new
        ...
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider an interface that has only one method: Object getValue(String data); Suppose this describes
Let's consider a data type with many constructors: data T = Alpha Int |
Consider a disk file containing 100 records a. How many comparisons would be required
Exceptions are wonderful things, but I sometimes worry that I throw too many. Consider
I know that there are many different architectures exist. In this question I consider
I have a class whose instances have attributes that are containers which themselves contain
Consider a Store that has_many Products which have_many Opinions. Here are Models: Store: class
Consider a class that has some events. This event list is going to grow.
Consider a production planning application with many products. Each product has a list of
I consider fluent interfaces very convenient for many tasks. But I feel uneasy when

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.