Skip to content

Instantly share code, notes, and snippets.

@nusco
Last active September 5, 2015 18:55
Show Gist options
  • Save nusco/535156 to your computer and use it in GitHub Desktop.
Save nusco/535156 to your computer and use it in GitHub Desktop.
Spell: Shared Scope
# ===================
# Spell: Shared Scope
# ===================
# Share variables among multiple contexts in the same Flat Scope (https://gist.github.com/nusco/535082).
lambda {
shared = 10
self.class.class_eval do
define_method :counter do
shared
end
define_method :down do
shared -= 1
end
end
}.call
counter # => 10
3.times { down }
counter # => 7
# 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