class Foo
def self.with_context
old = self.context #save current context
yield
@@context = old #return context back after method was run
end
def self.context=(value)
@@context = value
end
def self.context
@@context
end
def self.bar
with_context do
Foo.context = "baz"
puts Foo.context
end
end
end
Foo.context = "bar"
puts Foo.context
Foo.bar
puts Foo.context
$ ruby foo.rb
bar
baz
bar