From: Its Me Date: 2004-04-08T06:04:18+09:00 Subject: Re: Idea: Simplified GTK "Gregory Millam" wrote in message > 2) So it doesn't require defining a new child of EzGtk Either done with a class variable on class Object, as it is above, or use a global. But using either isn't thread-safe, so Thread.current["storefunc"] would be thread safe. - This is also, as the website 'Its me' pointed out, a dynamically scoped variable. > > So to accomplish (1), I'd just need to change my code to do this: > > holdfunc = Thread.current["storefunc"] > Thread.current["storefunc"] = lambda { ... } > yield thewidget > Thread.current["storefunc"] = holdfunc This would be another implementation basis for dynamically scoped vars. As the pragprog site details, you would have to wrap some push/pop around a stack of variable bindings, hold onto that stack in a Thread.current variable, and then: with_context (bindings) {...} # push onto dynamic bindings stack find_in_context(var) # lookup bindings stack from innermost to outer One advantage this approach offers over Avi's callcc/catch/throw gem is that you could, should such nastiness grab your fancy, even do the lookup in the opposite direction. to make an outer binding can override an inner binding: with_context(:var=>"outer") { with_context(:var=>"inner") { find_in_context(:var) # ===> "outer" } } btw: I think some form of dynamic binding should be in the Ruby standard libraries, just like resumable exceptions (whose implementation would probably also use callcc/catch/throw).