Skip to content

Instantly share code, notes, and snippets.

@sunaot
Created February 10, 2012 06:54
Show Gist options
  • Save sunaot/1787277 to your computer and use it in GitHub Desktop.
Save sunaot/1787277 to your computer and use it in GitHub Desktop.
local static variable in Ruby, using file local scope.
# g.rb
cache = ""
klass = class<<self; self; end
klass.send(:define_method, :foo) do
lambda {|s| cache += s; cache }
end
# call_foo.rb
require './g'
f = foo
f.call "a"
puts f.call("b")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment