# Transparent Lazy execution

**URL:** <https://www.ruby-forum.com/t/transparent-lazy-execution/131965>\
**Category:** Ruby\
**Created:** [March 14, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/transparent-lazy-execution/131965 "2008-03-14T20:00:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xmg-eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f04885/32.png) [@xmg-eric](https://www.ruby-forum.com/u/xmg-eric)\
**Post date:** [March 14, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/transparent-lazy-execution/131965/1 "2008-03-14T20:00:40Z")

</div>

I have often wanted to execute a bit of code only if the result of  
that code is used later. For example in a Rails app I might load some  
data in my controller but sometimes depending on some situations the  
result is not actually used in the view.

So what I want is to be able to do:

@recent = lazy { Post.find\_recent 5 }

But then if @recent is never used the actual search is not executed.  
But because I don’t want to break compatibility once @recent is used I  
want it to act and behave exactly like it is the return value of the  
block. Even the core methods like class and respond\_to? should return  
the correct values.

So I took my first stab at it. You can see it at:

[http://files.pixelwareinc.com/transparent\_lazy.rb](http://files.pixelwareinc.com/transparent_lazy.rb)

My questions are:

1. What trouble am I getting myself into by allowing this object to  
completely loose it’s identity?
2. What holes are in the armor? Is there any way my proxy class does  
not act like the result of the block? If there are, do they matter?
3. What are the performance and memory implications?

The only hole I can think of is that you see the proxy if you are  
stepping through your code. This is no big deal to me.

There would be a slight memory increase as there is an extra object  
but it should be insignificant. And everything should be garbage  
collected when nothing is using the proxy object anymore so I don’t  
forsee any memory leaks. Obviously every call to the lazy-loaded data  
incurs an extra method call but I think I can live with that.

My next step will be to monkey-patch this into Rails so that  
ActiveRecord::Base.find will use the lazy method by default. This will  
allow everything to be lazy loaded without having to change a single  
line of code in my application.

---

<div class="post-metadata">

**Author:** ![xmg-eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f04885/32.png) [@xmg-eric](https://www.ruby-forum.com/u/xmg-eric)\
**Post date:** [March 14, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/transparent-lazy-execution/131965/2 "2008-03-14T23:26:24Z")

</div>

On Mar 14, 2:59 pm, Eric A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But because I don’t want to break compatibility once @recent is used I
> 
> 1. What trouble am I getting myself into by allowing this object to  
> collected when nothing is using the proxy object anymore so I don’t  
> forsee any memory leaks. Obviously every call to the lazy-loaded data  
> incurs an extra method call but I think I can live with that.
> 
> My next step will be to monkey-patch this into Rails so that  
> ActiveRecord::Base.find will use the lazy method by default. This will  
> allow everything to be lazy loaded without having to change a single  
> line of code in my application.

Have you looked at lazy.rb?

T.

---

<div class="post-metadata">

**Author:** ![xmg-eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f04885/32.png) [@xmg-eric](https://www.ruby-forum.com/u/xmg-eric)\
**Post date:** [March 15, 2008, 3:11am UTC](https://www.ruby-forum.com/t/transparent-lazy-execution/131965/3 "2008-03-15T03:11:00Z")

</div>

On Mar 14, 2008, at 12:59 PM, Eric A. wrote:

> @recent = lazy { Post.find\_recent 5 }

gem install fattr

fattr(:recent){ Post.find\_recent 5 }

[http://www.rubyquiz.com/quiz67.html](http://www.rubyquiz.com/quiz67.html)

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![xmg-eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/f04885/32.png) [@xmg-eric](https://www.ruby-forum.com/u/xmg-eric)\
**Post date:** [March 15, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/transparent-lazy-execution/131965/4 "2008-03-15T19:35:57Z")

</div>

On Mar 14, 6:23 pm, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Have you looked at lazy.rb?

Yes, I saw that but I didn’t notice until now that it had transparent  
evaluation of the promised block. I’ll have to look at this closer as  
it seems to have what I need and is probably much better tested. Plus  
I like it’s idea of the futures it has.

Eric
