Extract local variables from a block?

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs