I have a block of code that should be evaluated again at run-time if needed.
class Test
def initialize
@some_block = nil
end
def make_initial_attributes(&block)
# do stuff with the supplied block, and then store the block somewhere
# for later
end
def rebuild_attributes
# grab that stored block and evaluate it again
end
end
I have Test objects that are created at start-up, but then throughout the program, I may want them to “update” themselves by running whatever block I fed them with way back at start-up.
Maybe the state of the program has changed and so these Test objects will happily check a bunch of things and let them determine what to update their values with. Of course, the block is something I write so (I think) they shouldn’t be able to do things that I didn’t plan…
The example is a little strange. Basically is it possible to store a block of code (which is just a Proc I believe) and then re-evaluate it later.
What you request is exactly what blocks are for. You just use ‘call’ to the stored block. Here is an example: