19

I'm using Sinatra with Apache and Phusion-Passenger with "classic" style:

# config.ru
require 'sinatra'

configure do
    ....
end

require './app'

run Sinatra::Application

I want to define some things. What is the difference between defining it inside the configure block or outside?

# config.ru
require 'sinatra'

# A) Defining logger here
rack = File.new("logs/rack.log", "a+") 
use Rack::CommonLogger, rack

# B) Global variables here
LOGGER = Logger.new(...)

# C) Gem configuration here
DataMapper::Property::Boolean.allow_nil(false)

configure do
    # A) Or defining logger here?
    rack = File.new("logs/rack.log", "a+") 
    use Rack::CommonLogger, rack

    # B) Or global variables here?
    LOGGER = Logger.new(...)

    # C) Or gem configuration here?
    DataMapper::Property::Boolean.allow_nil(false)
    ....
end

require './app'

run Sinatra::Application

Are there some general rules what should be done outside and what should be done inside? What is the difference? I tested both variants, and both seemed to work equally well.

I know configure can be used to react on environment like this:

configure :development do
    ....
end

So it is useful for different environment configurations. This use case is clear, but what about general configurations for every environment? Where do I put them? Is this only a matter of style?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Markus
  • 5,667
  • 4
  • 48
  • 64

1 Answers1

12

This is in the first place a matter of environments and in the second place a matter of style. There is no difference where you put your configurations.

It does make your code a lot more readable (IMHO) if you put it into a block. Also it will let you add environment based options which you then put into their own respective blocks.

To sum it up, it's up to you :)

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
three
  • 8,262
  • 3
  • 35
  • 39
  • 2
    I just found [the source](http://rubydoc.info/gems/sinatra/1.3.3/Sinatra/Base.configure), and it really does nothing more than yielding the block with itself as an argument. So it's only a matter of style. – Markus Jan 19 '13 at 12:04