# Any way to avoid instantiating a class twice?

**URL:** <https://www.ruby-forum.com/t/any-way-to-avoid-instantiating-a-class-twice/176488>\
**Category:** Rails\
**Created:** [October 1, 2009, 7:56am UTC](https://www.ruby-forum.com/t/any-way-to-avoid-instantiating-a-class-twice/176488 "2009-10-01T07:56:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![spencer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@spencer](https://www.ruby-forum.com/u/spencer)\
**Post date:** [October 1, 2009, 7:56am UTC](https://www.ruby-forum.com/t/any-way-to-avoid-instantiating-a-class-twice/176488/1 "2009-10-01T07:56:04Z")

</div>

Hey everyone, I’m trying to find a work around for a performance problem  
I’ve been having.

I have a class that builds paths (among other things) dynamically to  
many files and folders depending on what sets of information the user  
accesses. Once built, this class instantiation contains all the  
information necessary for several pages (controllers/views) to operate.  
However at times it can take up to 10 seconds to process.

It could be incredibly time consuming to re-instantiate the class every  
time I load any related sub-views/controllers.

The information contained in the instantiation is too large to fit into  
a session variable and too complex to be passed back in a query string.  
Also I should note that it is different for each user, so it can’t be  
used as some sort of program-wide global variable.

Is there any way around this situation? Is there some way to keep the  
instantiated class alive so it can be used in subsequent actions? Any  
help would greatly be appreciated!

Thanks!

---

<div class="post-metadata">

**Author:** ![spencer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@spencer](https://www.ruby-forum.com/u/spencer)\
**Post date:** [October 1, 2009, 8:05am UTC](https://www.ruby-forum.com/t/any-way-to-avoid-instantiating-a-class-twice/176488/2 "2009-10-01T08:05:57Z")

</div>

Persist that information to some other storage engine on your server,  
per  
user. The database is the first place I’d think of. Using database  
sessions  
removes the session limit.

However, look into Memcached, MongoDB, CouchDB, and other options.  
They’re  
pretty cool.

Also, maybe have someone else take a look at the code you’re writing. It  
may  
just be that there’s a better way to do what you’re doing. Something  
doesn’t  
seem right about what you’re doing, but I’m not here to criticize,  
especially if I haven’t even seen your code.

Maybe get a mentor at [http://railsmentors.org](http://railsmentors.org) 🙂 It’s a free service,  
part  
of RailsBridge.

On Thu, Oct 1, 2009 at 12:56 AM, Spencer R. \<

---

<div class="post-metadata">

**Author:** ![spencer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@spencer](https://www.ruby-forum.com/u/spencer)\
**Post date:** [October 1, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/any-way-to-avoid-instantiating-a-class-twice/176488/3 "2009-10-01T21:55:31Z")

</div>

> Is there any way around this situation? Is there some way to keep the  
> instantiated class alive so it can be used in subsequent actions?

Sure, for the non user-specific parts, you can lazy-load and cache:

class Expensive

def self.share\_me  
@some\_val ||= expensive\_calculation  
end

end
