pguardiario
pguardiario

Reputation: 55012

Get sinatra environment from within instance method

What's the proper way to determine the environment? Right now I'm using:

class Main < Sinatra::Base
    get '/' do
        puts self.class.development?
        puts self.class.production?
    end
end

But it doesn't seem right.

Upvotes: 16

Views: 10135

Answers (3)

Siwei
Siwei

Reputation: 21617

puts Sinatra::Application.environment

#=> production (or test, development)

Upvotes: 2

tbuehlmann
tbuehlmann

Reputation: 9110

self.class.development? should actually work. These all work for me on Sinatra 1.3.1:

class Main < Sinatra::Base
  get '/' do
    puts Main.development?
    puts self.class.development?
    puts settings.development?
    puts settings.environment == :development
  end
end

Upvotes: 23

Jim Deville
Jim Deville

Reputation: 10672

I would use Sinatra::Base.development? or Sinatra::Base.production? since that is where the methods are coming from.

Upvotes: 33

Related Questions