简体   繁体   中英

How to use Proc.new

I was curious about this line:

set :views, Proc.new

What it do, and why is Proc.new used?

class ApplicationController < Sinatra::Base
  configure do
    set :views, Proc.new { File.join(root, "../views/") }
    enable :sessions unless test?
    set :session_secret, "secret"
  end
end

This proc is not a static thing, it takes a settings value root and evaluates a path to views. Here is a piece of documentation of Sinatra:

When the setting value is a Proc , evaluation is performed every time the setting is read so that other settings may be used to calculate the value

Once you set root value you don't need to set the full path for views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM