Skip to content

Instantly share code, notes, and snippets.

@benolee
Last active August 29, 2015 14:04
Show Gist options
  • Save benolee/536c549c3d20d404ede9 to your computer and use it in GitHub Desktop.
Save benolee/536c549c3d20d404ede9 to your computer and use it in GitHub Desktop.
require 'rack'
class StackFrame
def initialize app
@app = app
end
def call env
p [:call, self.to_s, env]
_env = env.reduce(:merge)
_env[:a] += 1
ret = @app.call(env + [_env])
ensure
p [:return, self.to_s, ret]
end
end
class App
def call env
p [:call, self.to_s, env]
ret = env.reduce(:merge)[:a]
ensure
p [:return, self.to_s, ret]
end
end
app = Rack::Builder.new do
use StackFrame
use StackFrame
run App.new
end
stack = [{}, {a: 0}]
a = app.call stack
puts "a is: #{a}"
# [:call, "#<StackFrame:0x007fd2848f2f60>", [{}, {:a=>0}]]
# [:call, "#<StackFrame:0x007fd2848f2f88>", [{}, {:a=>0}, {:a=>1}]]
# [:call, "#<App:0x007fd2848f3190>", [{}, {:a=>0}, {:a=>1}, {:a=>2}]]
# [:return, "#<App:0x007fd2848f3190>", 2]
# [:return, "#<StackFrame:0x007fd2848f2f88>", 2]
# [:return, "#<StackFrame:0x007fd2848f2f60>", 2]
# a is: 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment