In Sinatra, I\'m unable to create global variables which are assigned values only once in the application lifetime. Am I missing something? My simplified code looks like thi
I ran into a similar issue, I was trying to initialize an instance variable @a using the initialize method but kept receiving an exception every time:
class MyApp < Sinatra::Application
def initialize
@a = 1
end
get '/' do
puts @a
'inside get'
end
end
I finally decided to look into the Sinatra code for initialize:
# File 'lib/sinatra/base.rb', line 877
def initialize(app = nil)
super()
@app = app
@template_cache = Tilt::Cache.new
yield self if block_given?
end
Looks like it does some necessary bootstrapping and I needed to call super().
def initialize
super()
@a = 1
end
This seemed to fix my issue and everything worked as expected.