why cannot I use @env['REMOTE_ADDR'] outside a route in Sinatra?
I want to have client’s ip available for my whole Sinatra application so I can use it anywhere …
clients_ip = @env['REMOTE_ADDR']
get '/' do
.. show something ..
clients_ip
end
get '/page1' do
.. show something ..
clients_ip
end
Only the route blocks run in response to a request. The code outside is run once when the handler is set up.
Instead of using a variable you can do what you want with a method, this makes sure that the lookup in
@envis done in the correct scope.