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
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