why cannot开发者_C百科 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 @env is done in the correct scope.
def clients_ip
  @env['REMOTE_ADDR']
end
get '/' do
  # ... show something ...
  clients_ip
end
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论