Skip to content

Instantly share code, notes, and snippets.

@zzak
Forked from nusco/shared_scope.rb
Created September 13, 2010 16:53
Show Gist options
  • Save zzak/577588 to your computer and use it in GitHub Desktop.
Save zzak/577588 to your computer and use it in GitHub Desktop.
# ===================
# Spell: Shared Scope
# ===================
# Share variables among multiple contexts in the same Flat Scope (103).
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