RubyのフレームワークSinatraを触ってみた

Rubyフレームワークといえば、Railsが有名ですが、最近わりと頻繁にSinatraの名前を聞くので、ちょこっと触ってみました。

公式サイトはこちら。

ってことで、さっそくSinatraをインストールする。

$ sudo gem install sinatra

GitHub - sinatra/sinatra: Classy web-development dressed in a DSL (official / canonical repo)のサンプルコードを書いて動かしてみる。

require 'rubygems'
require 'sinatra'

get '/' do
    'Hello sinatra!'
end

起動してみる。

$ ruby hello.rb 
/opt/local/lib/ruby/gems/1.8/gems/sinatra-0.9.2/lib/sinatra/base.rb:930:in `detect_rack_handler': Server handler (thin,mongrel,webrick) not found. (RuntimeError)
        from /opt/local/lib/ruby/gems/1.8/gems/sinatra-0.9.2/lib/sinatra/base.rb:862:in `run!'
        from /opt/local/lib/ruby/gems/1.8/gems/sinatra-0.9.2/lib/sinatra/main.rb:34
        from hello.rb:4

どうやらthin, mongrel, webrickのいずれかが見つからないようだ。ということで、mongrelをインストールする。(mongrelを選んだ理由は特にない)

$ sudo gem install mongrel

再度起動してみる。

$ ruby hello.rb 
== Sinatra/0.9.2 has taken the stage on 4567 for development with backup from Mongrel

以下のURLにアクセスすると、Hello sinatra!と出力された。

また近いうちにもう少し触ってみようと思う。