Extract local variables from a block?

J

Josh French

Is it possible to extract local variables from within a block, while
standing outside said block? Here's an example which might illustrate
what I'm trying to do:

def local_vars_from(&block)
eval('local_variables', block).each do |var|
new_local_var = eval(var, block)
end
end

local_vars_from do
unknown_local_var = :blah
end

Of course, this example doesn't work at all. What I'm trying to
accomplish is a new variable in the current scope which mirrors the
variable inside the block. I'm pretty sure I'm misusing
local_variables; is there another approach to this?

Thanks,
j
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top