blocks and late evaluation

  • Thread starter Vassilis Vatikiotis
  • Start date
V

Vassilis Vatikiotis

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
 
R

Robert Klemme

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
 
F

Fritz Wuehler

Robert Klemme said:
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 ;-)
 
V

Vassilis Vatikiotis

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top