Sinatra をインストールし,使ってみる<。Sinatra のインストールは簡単です
In this Web pager, installing and using Sinatra is explained.
◆ Ubuntu での実行手順例 (Instal Sinattra on Ubuntu)
sudo apt -y update sudo apt -y install ruby sudo apt -y install ruby-dev sudo apt -y install rubygems sudo gem install sinatra
sudo apt -y update sudo apt -y install erubis sudo apt -y install erubis-doc sudo apt -y install liberubis-ruby
■ Classic スタイルの場合 (In case of "Classic Style" of Sinatra)
require 'rubygems' require 'sinatra' get '/hi' do erubis "Hello World!" end
ruby hi.rb
◆ Ubuntu での実行手順例 (In case of Ubuntu)
http://localhost:4567/hi
■ Moudlar スタイルの場合
require 'rubygems' require 'sinatra/base' class MyApp < Sinatra::Base get '/hi' do erubis "Hello World!" end end MyApp.run! :host => 'localhost', :port => 4567
ruby hi.rb
◆ Ubuntu での実行手順例
http://localhost:4567/hi
cd /tmp redis-server
require 'rubygems' require 'redis' require 'erubis' require 'sinatra/base' class MyApp < Sinatra::Base get '/hoge/new/key=:key&&field=:field&&value=:value' do @redis = Redis.new(:host => "127.0.0.1", :port => "6379", :db => "hoge") @redis.hset(params[:key], params[:field], params[:value]) end get '/hoge/show' do @redis = Redis.new(:host => "127.0.0.1", :port => "6379", :db => "hoge") @keys = @redis.keys(glob = "*") erb :show end get '/hoge' do @redis = Redis.new(:host => "127.0.0.1", :port => "6379", :db => "hoge") @keys = @redis.keys(glob = "*") erb :show end end MyApp.run! :host => 'localhost', :port => 4568
<HTML> <BODY> <table> <tr> <td width="150">key</td> <td width="150">field</td> <td width="150">value</td> </tr> <% @keys.each do |key| %> <% @fields_and_values = @redis.hgetall(key) %> <% @fields_and_values.each do |field_and_value| %> <tr> <td><%= key %></td> <td><%= field_and_value[0] %></td> <td><%= field_and_value[1] %></td> </tr> <% end %> <% end %> </table>