Created
April 12, 2013 10:14
-
-
Save astropanic/5371034 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby foo.rb | |
bar | |
baz | |
bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment