Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > blocks and late evaluation

Reply
Thread Tools

blocks and late evaluation

 
 
Vassilis Vatikiotis
Guest
Posts: n/a
 
      09-05-2011
hi all,

my question is in the context of sinatra but it's a blocks question really.

I have configure block in sinatra (configure blocks in sinatra run only once, upon initialisation). There, I want to load some database records in memory. So I do:
configure do
set :keys do
DB.all.map { |record| record.field }
end
end

Whenever I access the class level variable :keys I get a DB query. Whereas if, inside that configure block, I do:

keys = DB.all.map { |record| record.field }
set :keys, keys

there are no DB queries, I can even stop the database daemon and my app will go on.

Now I'm *almost* certain that it's not a sinatra issue but my ruby block ignorance. I know that Proc objects are evaluated when they are called but here we have a block returning a new Array, and not a Proc object.

can you explain it?
thx
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      09-05-2011
On 05.09.2011 12:46, Vassilis Vatikiotis wrote:
> hi all,
>
> my question is in the context of sinatra but it's a blocks question really.
>
> I have configure block in sinatra (configure blocks in sinatra run
> only once, upon initialisation). There, I want to load some database
> records in memory. So I do:
> configure do
> set :keys do
> DB.all.map { |record| record.field }
> end
> end
>
> Whenever I access the class level variable :keys I get a DB query.
> Whereas if, inside that configure block, I do:
>
> keys = DB.all.map { |record| record.field }
> set :keys, keys
>
> there are no DB queries, I can even stop the database daemon and my app will go on.
>
> Now I'm *almost* certain that it's not a sinatra issue but my ruby
> block ignorance. I know that Proc objects are evaluated when they are
> called but here we have a block returning a new Array, and not a Proc
> object.
>
> can you explain it?
> thx


I don't know Sinatra (apart from Frank, of course) but the reason must
either be in DB.all's or in set's behavior. To debug it might be
helpful to insert something like "$stderr.puts 'called'" inside the
block so you can see how often the block is invoked.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
 
Fritz Wuehler
Guest
Posts: n/a
 
      09-05-2011
Robert Klemme <(E-Mail Removed)> wrote:

> I don't know Sinatra (apart from Frank, of course) but the reason must
> either be in DB.all's or in set's behavior. To debug it might be
> helpful to insert something like "$stderr.puts 'called'" inside the
> block so you can see how often the block is invoked.


That's correct. For Sinatra he should have used DB.all.or.nothing.at.all.

and $when.its.love(there) /= in.between()

I'm surprised somebody else didn't catch this

 
Reply With Quote
 
Vassilis Vatikiotis
Guest
Posts: n/a
 
      09-06-2011
It's sinatra related after all. set stores the block object and every time :keys is referenced it calls the block.
sorry for of topic and thx
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird exception handling behavior -- late evaluation in except clause Roy Smith Python 6 12-03-2012 07:38 AM
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Python Ilias Lazaridis Python 2 04-24-2005 05:29 PM
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby Ilias Lazaridis Ruby 74 04-04-2005 05:29 PM
procs/blocks - blocks with procs, blocks with blocks? matt Ruby 1 08-06-2004 01:33 AM



Advertisments