Today let’s look at the carrierwave gem’s main module. The interesting thing is to see how this Gem prepares for environments in many frameworks, not only Rails. if defined?(Merb)   CarrierWave.root = Merb.dir_for(:public) Merb::BootLoader.before_app_loads do # Setup path for uploaders and load all of them before classes are loaded Merb.push_path(:uploaders, Merb.root / ‘app’ / ‘uploaders’, ‘*.rb’) Dir.glob(File.join(Merb.load_paths[:uploaders])).each {|f| require f } end   elsif defined?(Rails)   module CarrierWave class Railtie < Rails::Railtie initializer "carrierwave.setup_paths" do CarrierWave.root = Rails.root.join(Rails.public_path).to_s CarrierWave.base_path = ENV[’RAILS_RELATIVE_URL_ROOT’] end   initializer "carrierwave.active_record" do ActiveSupport.on_load :active_record do require ‘carrierwave/orm/activerecord’ end end end end   elsif defined?(Sinatra) if defined?(Padrino) && defined?(PADRINO_ROOT) CarrierWave.root = File.join(PADRINO_ROOT, "public") else   CarrierWave.root = if Sinatra::Application.respond_to?(:public_folder) # Sinatra >= 1.3 Sinatra::Application.public_folder else # Sinatra[…]

Sinatra the light weight framework for Ruby can spin up a quick Rack app for use in minutes. Here I show how to enable :sessions in your Sinatra application. 1) Install the Sinatra gem and create the application. # ~/Code/sessioned_sinatra.rb require ‘sinatra’ enable :sessions   get ‘/’ do erb :index end   get ‘/authorize’ do if session[:requesting_site] session.clear else session[:requesting_site] = "http://brettu.com" end erb :index end   get ‘/reset’ do session.clear erb :index end   __END__   @@index <html> <head> <title>Sinatra Session Demo</title> </head> <body> <h4>Holy Cats Batman, a real site</h4> <% if session[:requesting_site] %> <p>You have this session: <%= session.inspect %></p> <a href="/reset">Please reset</a> <% else %> <p>To activate your session:</p> <a href="/authorize">Click here</a> <% end %> </body> </html>[…]

Sometimes connecting to a Database or other storage software can be risky and it’s good to have a backup. PStore is a Ruby class available to use for these cases. It writes to file which Ruby can easily access if needed. 1) Create an inbox.rb file in a pstore directory ➜ mkdir pstore ➜ cd pstore ➜ pstore vim inbox.rb 2) Add the following to the file 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 require "pstore"   class Inbox   def initialize(file="doc.pstore") @file = file end   def store @store ||=[…]