Skip to content

Instantly share code, notes, and snippets.

@a2ikm
Created October 15, 2025 14:26
Show Gist options
  • Select an option

  • Save a2ikm/fef76a2ad3ea082df3002ad65299a121 to your computer and use it in GitHub Desktop.

Select an option

Save a2ikm/fef76a2ad3ea082df3002ad65299a121 to your computer and use it in GitHub Desktop.
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "grape"
gem "webrick"
gem "rackup"
gem "builder"
end
require "rackup/handler/webrick"
require "net/http"
$gvar = nil
class Foo < Grape::API
get '/' do
@ivar = 1
{ok: true}
end
end
class Bar < Grape::API
before do
@ivar = 0
end
finally do
$gvar = @ivar
end
mount Foo
end
Thread.new do
Rackup::Handler::WEBrick.run(Bar, Port: 9292)
end
begin
Net::HTTP.get('localhost', '/', 9292)
rescue => e
sleep 1
retry
end
puts "$gvar == 1 #=> #{$gvar == 1}"
@a2ikm
Copy link
Author

a2ikm commented Oct 15, 2025

I think that the before and finally blocks are shared to Foo by mount, and are evaluated in an instance of Foo.

(Updated)
It is a bit wrong.
finally seems to be evaluated in an anonymous subclass of Grape::Endpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment