Created
August 18, 2010 15:15
-
-
Save nusco/535082 to your computer and use it in GitHub Desktop.
Spell: Flat Scope
This file contains 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
# ================= | |
# Spell: Flat Scope | |
# ================= | |
# Use a closure to share variables between two scopes. | |
class C | |
def an_attribute | |
@attr | |
end | |
end | |
obj = C.new | |
a_variable = 100 | |
# flat scope: | |
obj.instance_eval do | |
@attr = a_variable | |
end | |
obj.an_attribute # => 100 | |
# For more information: http://www.pragprog.com/titles/ppmetr/metaprogramming-ruby |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment