LoginSignup
3
3

More than 5 years have passed since last update.

Sinatra::Base はルーティングや設定も継承する

Posted at

Sinatra::Base を継承したクラスは、

  • 親クラスのルーティングをさらに継承したクラスでも利用できます
  • 親クラスの設定をさらに子クラスでも参照できます
app.rb
require 'sinatra/base'

class Parent < Sinatra::Base
  get '/' do
    "original root"
  end

  get '/foo' do
    "original foo"
  end

  set :some_setting, "some text"
end

class Child < Parent
  get '/foo' do
    "override foo"
  end

  get '/buz' do
    "#{settings.some_setting}"
  end
end

Child.run!

====

GET /

====

GET /foo

====

GET /buz

route! が親クラスを遡って探して行くため && settings は要するに self なので、普通に親クラスに定義されやメソッドを見に行くため、です

3
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3