六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 139|回复: 0

一步一步学sinatra

[复制链接]

升级  84%

10

主题

10

主题

10

主题

童生

Rank: 1

积分
42
 楼主| 发表于 2013-2-7 21:10:13 | 显示全部楼层 |阅读模式
找到开始:


module Sinatra  class Application < Base    puts "aaaaa"    # we assume that the first file that requires 'sinatra' is the    # app_file. all other path related options are calculated based    # on this path by default.    set :app_file, caller_files.first || $0    set :run, Proc.new { $0 == app_file }    if run? && ARGV.any?      require 'optparse'      OptionParser.new { |op|        op.on('-x')        {       set :lock, true }        op.on('-e env')    { |val| set :environment, val.to_sym }        op.on('-s server') { |val| set :server, val }        op.on('-p port')   { |val| set :port, val.to_i }        op.on('-o addr')   { |val| set :bind, val }      }.parse!(ARGV.dup)    end  end  at_exit { Application.run! if $!.nil? && Application.run? }en

  首先看看at_exit.....

http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html#Kernel.at_exit

其实就是跑Sinatra.Base.run


经常用set,看一下:


      # Sets an option to the given value.  If the value is a proc,      # the proc will be called every time the option is accessed.      def set(option, value = (not_set = true), &block)        raise ArgumentError if block and !not_set        value = block if block        if value.kind_of?(Proc)          metadef(option, &value)          metadef("#{option}?") { !!__send__(option) }          metadef("#{option}=") { |val| metadef(option, &Proc.new{val}) }        elsif not_set          raise ArgumentError unless option.respond_to?(:each)          option.each { |k,v| set(k, v) }        elsif respond_to?("#{option}=")          __send__ "#{option}=", value        else          set option, Proc.new{value}        end        self      end
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表