From: Joel VanderWerf Date: 2003-06-03T16:04:08+09:00 Subject: Re: Dynamically scoped variables Dave Thomas wrote: > Just as a bit of fun I posted a piece on implementing dynamically scoped > variables in Ruby at > > http://pragprog.com/pragdave/Tech/Random/StackContext.rdoc,v Very cool! Here's a variant that lets you access local variables from nested dynamic scopes, though I'm not sure that's a good idea :) def with_current_context finder = catch(:context) { yield } finder.call(Proc.new.binding) if finder ## ? Is there an easier way to get caller's binding? end def find_in_context(name) callcc do |again| throw(:context, proc {|b| begin val = eval(name, b) again.call(val) rescue NameError raise "Can't find context value for #{name}" end }) end end def update_widget name = find_in_context("name") color = find_in_context("color") puts "<#{color}>#{name}" end def update_screen update_widget end name = 'dave'; color = 'red' with_current_context do update_screen end